in reply to Alternation in pattern matches

If you do include code, and it's output is important to understand the question, then please be so kind to include the output!

The question arises, are you a backslashophile? You escape things that aren't nessary, making it harder to read the code.

Anyway, a way of doing it in one regexp (no alternation, that would be hard unless you want to switch afterwards):

my (undef, $data) = /TAG:("?)((??{ $1 ? '[^"]+' : '[^"\s]+' }))\1/; $data //= 'not_def';

Abigail

Replies are listed 'Best First'.
Re: Re: Alternation in pattern matches
by ysth (Canon) on Feb 19, 2004 at 01:40 UTC
    You don't like (")?(?(1)[^"]+|[^"\s]+) ?

      It just won't catch a missing closing qquote, as in TAG:"test of data. However, you can correct that like: my ( $data ) = m/TAG:(")?((?(1)[^"]+|[^"\s]+))(?(1)")/ ? $2 : 'not_def';

        Sorry, was just being overly terse; in full, that would be:
        /TAG:(")?((?(1)[^"]+|[^"\s]+))(?(1)")/ ? $2 : "not_def"
        (I had thought that \1 would work instead of (?(1)")), but it makes sense that it doesn't.)