in reply to This regexp made simpler

Not extensively tested, but does
my @strings = ('AZ', 'A SOMETHING Z', 'ASOMETHINGZ', 'A Z', 'A ZZ', ' +AA ZZ', 'AAZZ'); for (@strings) { if (/A( .*?)?Z/) { my $grabbed = $1 // ''; say "'$_' grabbed '$grabbed'"; } else { say "'$_' did not match" } } __END__ 'AZ' grabbed '' 'A SOMETHING Z' grabbed ' SOMETHING ' 'ASOMETHINGZ' did not match 'A Z' grabbed ' ' 'A ZZ' grabbed ' ' 'AA ZZ' grabbed ' ' 'AAZZ' grabbed ''

do what you want?

Update

What should 'A ZZ', 'AAZZ' and 'AA ZZ' match? (added these as test cases)


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: This regexp made simpler
by AnomalousMonk (Archbishop) on Apr 25, 2010 at 11:14 UTC

    Contrary to my interpretation of the requirements of the OP, both your regex and the updated regex of rovf's OP allow a 'Z' between the first 'A' and the final 'Z', and also still need to have an undefined  $1 rationalized to an empty string.

Re^2: This regexp made simpler
by rovf (Priest) on Apr 25, 2010 at 11:19 UTC
    What should 'A ZZ', 'AAZZ' and 'AA ZZ' match? (added these as test cases)
    They would (and should) not match at all...

    -- 
    Ronald Fischer <ynnor@mm.st>
      Updating my post to accommodate the anchors and your update:
      my @strings = ('AZ', 'A SOMETHING Z', 'ASOMETHINGZ', 'A Z', 'A ZZ', ' +AA ZZ', 'AAZZ', 'A Z'); for (@strings) { if (/^A( [^Z]*)?Z$/) { my $grabbed = $1 // ''; say "'$_' grabbed '$grabbed'"; } else { say "'$_' did not match" } } __END__ 'AZ' grabbed '' 'A SOMETHING Z' grabbed ' SOMETHING ' 'ASOMETHINGZ' did not match 'A Z' grabbed ' ' 'A ZZ' did not match 'AA ZZ' did not match 'AAZZ' did not match 'A Z' grabbed ' '


      Unless I state otherwise, all my code runs with strict and warnings
      I should have taken more notice of the anchors in your OP :(