Hi,
Disclaimer: I am not a regex wizzard, so I'm not sure if the following has any pitfalls, but it does appear to be possible with a single regex:
print $_, /(x(.*)x(??{ '.{'.length($2).'}' })x)/ ? " matches, \$1 = $1\n" : " doesn't match\n" for qw/ xxx x.x.x x12x..x x123x...x x1x2x...x x123x.x.x x12x1x ax1x2xbx34x56xc /; __END__ xxx matches, $1 = xxx x.x.x matches, $1 = x.x.x x12x..x matches, $1 = x12x..x x123x...x matches, $1 = x123x...x x1x2x...x matches, $1 = x1x2x...x x123x.x.x matches, $1 = x123x.x.x x12x1x doesn't match ax1x2xbx34x56xc matches, $1 = x1x2xbx34x56x
Update: Changing the first part of the regex to x(.*?)x (non-greedy) will allow you to match all the substrings in that last example above (and the rest of the examples above will continue to work the same):
my $re = qr/(x(.*?)x(??{ '.{'.length($2).'}' })x)/; my $str = "ax1x2xbx34x56xc"; while ($str=~/$re/g) { print "found \"$1\"\n"; } __END__ found "x1x2x" found "x34x56x"
Hope this helps,
-- Hauke D
In reply to Re: Regular Expression: search two times for the same number of any signs (updated)
by haukex
in thread Regular Expression: search two times for the same number of any signs
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |