in reply to This regexp made simpler
>perl -wMstrict -le "for (@ARGV) { if(/^A (?: Z | (\s.*?)) Z$/x) { my $grabbed = $1 // ''; print qq{matched '$_' grabbed '$grabbed'}; } } " AZ AZZ AXZ "A SOMETHING Z" ASOMETHINGZ matched 'AZZ' grabbed '' matched 'A SOMETHING Z' grabbed ' SOMETHING '
I wonder why it is necessary to match something like 'AZZ' and yet grab an undefined value from it, which must later be rationalized Update: to an empty string. (Additionally, the regex Update: first regex
of the OP does not match 'AZ', which seems to be required by the OP.)
Wouldn't it make more sense only to grab stuff from strings that match? E.g., "if there is anything between A and Z, it must begin with a space and be followed by zero or more non-Z characters". (Has the advantage of matching 'AZ', no defined test needed.)
>perl -wMstrict -le "for (@ARGV) { if(/^A ((?: \s [^Z]*)?) Z$/x) { print qq{matched '$_' grabbed '$1'}; } } " AZ AZZ AXZ "A ZZ" "A SOMETHING Z" ASOMETHINGZ "A Z" "A Z" matched 'AZ' grabbed '' matched 'A SOMETHING Z' grabbed ' SOMETHING ' matched 'A Z' grabbed ' ' matched 'A Z' grabbed ' '
Updates:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: This regexp made simpler
by rovf (Priest) on Apr 25, 2010 at 11:16 UTC |