Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Problem with regexp to match

by AnomalousMonk (Archbishop)
on Oct 14, 2021 at 22:25 UTC ( [id://11137556]=note: print w/replies, xml ) Need Help??


in reply to Problem with regexp to match

When matching patterns with regexes, the first task is to figure out what the pattern is that you want to match. From the examples you've given us, it seems as if the pattern is

++--------+-------++--------+---- one or more digits || | || | VV V VV V 1/LC-13/E10GbE-1,01/LC-14/E10GbE-1 A A A A | | | | +---------+-------+---------+---- if preceded by hyphen/dash ++--------+---- one or more digits || | VV V 1/LC-13/E10GbE-1 A A | | +---------+---- if preceded by hyphen/dash

This solution is a variation on that of choroba given here. It introduces the idea of the lookaround assertion (the (?<= -) expression) and uses the fact that the /g modifier operating in list context allows return of the entire match without using capturing parentheses.

Win8 Strawberry 5.8.9.5 (32) Thu 10/14/2021 17:41:51 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings for my $str ( '01/LC-13/E10GbE-1,01/LC-14/E10GbE-1', '01/LC-13/E10GbE-1', ) { my @m = $str =~ m{ (?<= -) \d+ }xmsg; print "'$str' ->"; print " '$_'" for @m; print "\n"; } ^Z '01/LC-13/E10GbE-1,01/LC-14/E10GbE-1' -> '13' '1' '14' '1' '01/LC-13/E10GbE-1' -> '13' '1'
Please see perlre, perlretut, perlreref and perlrequick.


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://11137556]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found