in reply to Determing what part of a regex matched.

$regex = '(\w+)|(\d+)|(\s+)|([^\w\d\s]+)'; $text = 'The world is foo 2!'; while ( $text=~s/^$regex// ) { print '\w+',$/ if $1; print '\d+',$/ if $2; print '\s+',$/ if $3; print '[^\w\d\s]+',$/ if $4; }
$regex = '(\w+)|(\d+)|(\s+)|([^\w\d\s]+)'; $text = 'The world is foo 2!'; while ( $text=~s/^$regex// ) { print '\w+',$/ if defined $1; print '\d+',$/ if defined $2; print '\s+',$/ if defined $3; print '[^\w\d\s]+',$/ if defined $4; }
You could do something like the above, where $1 .. $4 deal represent which set of parens matched. Note that I changed the .*? to [^\w\d\s]+, as I assumed this is what you meant, as .*? will try to match nothing first (which it will always do and replace it with nothing (as per s/// operation). So your code was probably looping forever.

Also the $1 was not printing as there were no capturing parens in your regular expression.

update: striked out old code, and updated it as per Abigail-II's suggestion below.

-enlil

Replies are listed 'Best First'.
Re: Determing what part of a regex matched.
by Abigail-II (Bishop) on Mar 06, 2003 at 01:15 UTC
    You'd have to used defined $1 etc, otherwise, it'll fail to deal with " 0 ".

    Abigail

Re: Re: Determing what part of a regex matched.
by Anonymous Monk on Mar 06, 2003 at 03:37 UTC

    Yes you're right. Shame on me for write a quick example without running it first.

    An interesting thought. Wouldn't it be kind of brittle if the regex introduced another () all of the numbers would have to be shifted? My regexs are more complex then these and occasionally have a few parens in them.