Besides looking for documentation I thought you'd perhaps wanted an explanation why Perl's current behaviour is sane and to be expected, and given a way to do achieve what you thought Perl would do for you. Let me set my general reply in context of MAC address parsing:
First things first though:
local $_ = join ':', qw/0 0A 0C B B8 F/; # $mac
my $part = qr/[0-9A-Z]{1,2}/;
First you used
my @parts = /^($part):($part):($part):($part):($part):($part)$/;
which worked. Then you tried to shrink it to
my @parts = /
^
(?:
($part)
:
){5}
($part)
$
/x;
but that didn't work. Now, using "my" technique you just need to add three to four lines to achieve what you want.
use re 'eval'; # Needed due to interpolation of $part
my @parts;
/
(?{ local @_parts })
^
(?:
($part)
:
(?{ local @_parts = (@_parts, $1) })
){5}
($part)
$
(?{ @parts = (@_parts, $2) })
/x;
The beauty of this technique is that you don't have to know how many times you need/want to match; something that is required if you use the
x operator.
If you just want to solve this particular problem, why not simply verify with your second more compact regex and then
split it up on
/:/?
Update:Since I got negative response on this reply I reworded the beginning to make it better express what I meant. If it sounded offensive or bad in any way then that wasn't how it was meant and I apologize.
ihb
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.