http://qs321.pair.com?node_id=11137556


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:  <%-{-{-{-<