Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Problem with regexp to match

by AnomalousMonk (Bishop)
on Oct 14, 2021 at 22:25 UTC ( #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 (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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2022-01-17 11:28 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.