develooper Front page | perl.perl5.porters | Postings from September 1999

qr{ } and (?=..)

Thread Previous | Thread Next
From:
Ed Peschko
Date:
September 4, 1999 20:42
Subject:
qr{ } and (?=..)
Message ID:
37D1E69B.ECAE6991@csgsystems.com
I've been playing around with qr{ } and I've found it able to do stuff
like:

$qr = qr{ ... (?p{ $qr }) ...}x;

my $b = "otherstuff $qr";
my ($pattern) = ($line =~ m"($b)");

and work as expected. And more complex compound regular expressions work
fine too::

$qr  = qr{ ... (?p{ $qr })  ...}x;
$qm = qm{ ...(?p{ $qm }) ...}x;

my $compiled = "$qr\w+$qm";
my ($pattern) = ($line =~ m"($compiled)");

However, there are a couple of things that I've noticed so far:

1) (?=$compiledregex) doesn't work. A pattern that matches $re will
return null if (?=) is
    wrapped around it.

2) matching parens inside of the qr, such as:
    $regex = qr{ (... (?p{ $qr }) ...) }x;
    or
    $regex = qr{ ... (?p{ ( $qr ) }) ... }x;

    seem to match only once, ie: the 'topmost' match. It would be IMO
much more
    interesting and useful if they set $1, $2, $3, $4, $5, etc. so a
match on
    '( a ( b c ) ( d e ) )' would set:
    $1 == '(a ( b c ) ( d e ) )'
    $2 == '( b c )'
    $3 == '( d e )'
    etc.

3) the (?{   }) syntax is rather delicate. If there is a space either a)
between the beginning
    ? and { or b) between the ending } and ), perl complains of a 'not
terminated
    sequence'. I'm assuming this is because the code is not really
interpolated, but uses '})'
    as a end-post marker. If so, adding an explanatory note onto the
error would be really
    helpful in tracking stuff down. Since 'x' is used all the time,
having an explanation
    why (?{  } ) is wrong is a good idea IMO.

Regarding point #2, is there an array which holds a synonym to '$1'
'$2', etc? If your
regular expressions can have an indeterminant number of return values to
them, I think this
would be an easy way to keep track of them.

Ed


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About