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: <%-{-{-{-<
-
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 How to display code and escape characters
are good places to start.
|