Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Problem with regexp to match by AnomalousMonk
in thread Problem with regexp to match by logangha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-05-22 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (79 votes). Check out past polls.

    Notices?