Mailing List Archive

?= syntax and backreferences
Re: ?= syntax and backreferences [ In reply to ]
Strange sunspot activity caused epeschko@animas.tcinc.com (Ed Peschko) to write
:
| The first seems to be the case -- however in perlref I see no mention of the
| fact that (?=pattern) does not make backreferences..

I would expect to find anything in perlref "Perl References and Data
Structures". However, you might find perlre "Perl regular expressions" to
be of use. ;-)

Perlre does mention that (?=) is zero width and doesn't add to $&:

A zero width posative lookahead assertion. For example,
/\w+(?=\t)/ matches word followed by a tab, without including
the tab in $&.

I'll grant you that this isn't the best descrption. Since my ISPs internal
services (news, mail, etc) seem to be down and Sprintnet is having problems
(I'm seeing delays of three to five seconds between bursts of characters),
I'll see if I can provide a quick patch for perlre.pod.

| while ($text =~ /\b(([a-z]+)(?=\s.*.*?\;))/sg) {
| $statement = $1;
| $bareword = $2;
| if ($perl_word{$bareword} == $TRUE) {
| push(@perl_expressions, $statement);
| $text =~ /\;/sg;
| }
| }
|
| This *should* give a list of all the perl expressions in a program with high
| probability (and assuming no syntax errors in the statement).

I'm not sure I see the reasoning behind your use of an assertion here. Or,
for that matter, your RE in general. You are asserting that you have one
or more lowercase characters folloed by a space and anything and anything
minimally and a semicolon (no need to escape it)?

-spp
Re: ?= syntax and backreferences [ In reply to ]
Re: ?= syntax and backreferences [ In reply to ]
On Mon, 20 Nov 1995, Ed Peschko wrote:

> hey --
>
> I was wondering (another bug/feature thing) if ?= was supposed to leave
> backreferences... ie:
>
> $text =~ /(text (?=lookahead_text))/;
>
> ie -- should:
>
> print $1;
>
> print out:
>
> text
>
> or print out:
>
> text lookahead text
>
>
> The first seems to be the case -- however in perlref I see no mention of the
> fact that (?=pattern) does not make backreferences..

Wrong and right. (?=...) is negative width, but does leave
backreferences. See this:

~$ perl -e '$_ = "abc"; m/(a(?=b(c)))/; print "$1, $2, $3, $4\n"'
a, c, ,


You see, by the time it gets to the outermost closing paren (to finish
$1) the position has been reset to just after "a". But within the (?=...)
the parens get closed normally, and thus $2 arises.

> Ed
>
> PS: I am using this in an iterative context to find perl expressions, ie:
>
>
> while ($text =~ /\b(([a-z]+)(?=\s.*.*?\;))/sg) {
> $statement = $1;
> $bareword = $2;
> if ($perl_word{$bareword} == $TRUE) {
> push(@perl_expressions, $statement);
> $text =~ /\;/sg;
> }
> }
>
> This *should* give a list of all the perl expressions in a program with high
> probability (and assuming no syntax errors in the statement).
>
> Ed
>

--
Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)