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:

  1. However, the 'Z' still needs to be repeated in the regex! Oh, well...
  2. Added "A Z" and "A  Z" test cases to my solution.

Replies are listed 'Best First'.
Re^2: This regexp made simpler
by rovf (Priest) on Apr 25, 2010 at 11:16 UTC
    I wonder why it is necessary to match something like 'AZZ' and yet grab an undefined value from it.
    Good point. This made me rethink my problem. In my case, the grabbed part is not really kept in a variable (I wrote it in that way in the hope to make the whole posting simpler), but within a substituion (to be precise, an insertion): I need to change a text AXZ into AXIZ, where the X is optional. In otherwords, I have to insert I in front of the Z, so in the substitution I use

    s/..../A$1IZ/
    , and if I know that $1 is always defined, I don't have to care about interpolating an undefined value. In hindsight, I now see that I should better have written

    s/^(A(?:\s.*?)?(Z))/$1I$2/
    . :-(
    -- 
    Ronald Fischer <ynnor@mm.st>