Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Perl Regex

by AnomalousMonk (Archbishop)
on Nov 17, 2019 at 06:12 UTC ( [id://11108812]=note: print w/replies, xml ) Need Help??


in reply to Perl Regex

Here's another approach:

c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version $]}; ;; my @strings = ( 'Link:1625 housing Link:2004', 'Link:638 92-5000|Link:63892-5070', ); ;; my $rx_numeric = qr{ \d+ (?: - \d+)? }xms; ;; for my $str (@strings) { my @matches = $str =~ m{ Link : ($rx_numeric (?: \s+ $rx_numeric)?) + }xmsg; print qq{'$str' -> }, map qq{'$_' }, @matches; } " perl version 5.008009 'Link:1625 housing Link:2004' -> '1625' '2004' 'Link:638 92-5000|Link:63892-5070' -> '638 92-5000' '63892-5070'
Add the  /i modifier if you want case insensitive matching:
    my @matches = $str =~ m{ ... }xmsgi;
For  qr// see Regexp Quote-Like Operators in perlop. For  =~ see Binding Operators in perlop. See also perlre, perlretut, and perlrequick.

Update: I often find it prudent to add an extra element of safety by using boundary assertions in data field regexes. So  $rx_numeric might be better defined as (tested)
    my $rx_numeric = qr{ (?<! \d) \d+ (?: - \d+)? (?! \d) }xms;
See  (?<!pattern) (?!pattern) in Extended Patterns in perlre.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108812]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-03-29 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found