Extended regexes - "(?<...)" construct.

C

Cloink

Can anyone tell me why my version of Perl won't compile a so-called
positive lookbehind assertion.

Specifically, I want to strip JavaScript comments from a file, so I'm
looking for "//" on a line, but since JS code can use regexes too,
it's possible to have "//" in a js-regex, which clearly isn't a
comment.

If the "//" is preceded by backslash however, i.e. "\//", I can be
pretty sure that this does NOT represent a comment. (I don't need
advice on when this rule might break - I have a very specific
purpose.)

Here's one piece of problematic (javascript) code
.split(/\//); // split string at forward-slash delimiters.

My PERL regex, in attempting to identify a preceding backslash in the
js code, I WANT to look like this
s/(?<[^\\])\/\/.*$//x
to identify two slashes on a line but not immediately preceded by a
backslash.

But the perl compiler tells me it doesn't understand:-
Sequence (?<[...) not recognized in regex; [line cont..]
marked by <-- HERE in m/(?<[ <-- HERE ^\\])//.*$/

Incidentally, if I replace the awkward "not-a-backslash" section of
the regex with a more simple "an x" section, I still get the same
compilation error, i.e.
replace "[^\\]" with "x"

"perl -v" tells me
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
and
Binary build 820 [274739] provided by ActiveState
http://www.ActiveState.com
Built Jan 23 2007 15:57:46

It's not my reference book that's wrong is it? (Perl in a Nutshell,
2nd Ed, June 2002, O'Reilly.)

Thanks.
 
X

xhoster

Cloink said:
My PERL regex, in attempting to identify a preceding backslash in the
js code, I WANT to look like this
s/(?<[^\\])\/\/.*$//x
to identify two slashes on a line but not immediately preceded by a
backslash.

In that case, wouldn't it make more sense to use negative look-behind?
If the line starts with //, then it is not preceded by a backslash, but
it is not preceded by a non-backslash, either.

But the perl compiler tells me it doesn't understand:-
Sequence (?<[...) not recognized in regex; [line cont..]
marked by <-- HERE in m/(?<[ <-- HERE ^\\])//.*$/

Incidentally, if I replace the awkward "not-a-backslash" section of
the regex with a more simple "an x" section, I still get the same
compilation error, i.e.
replace "[^\\]" with "x"

"perl -v" tells me
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
and
Binary build 820 [274739] provided by ActiveState
http://www.ActiveState.com
Built Jan 23 2007 15:57:46

It's not my reference book that's wrong is it? (Perl in a Nutshell,
2nd Ed, June 2002, O'Reilly.)

The perldoc perlre says the positive look behind syntax is (?<=pattern).
If your reference book omits then "=", then it is wrong.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
M

Mumia W.

Can anyone tell me why my version of Perl won't compile a so-called
positive lookbehind assertion.

Specifically, I want to strip JavaScript comments from a file,
[...]
But the perl compiler tells me it doesn't understand:-
Sequence (?<[...) not recognized in regex; [line cont..]
marked by <-- HERE in m/(?<[ <-- HERE ^\\])//.*$/
[...]

Look closely at "perldoc perlre". (?< ...) is not supported. You can use
one of these:

(?<= ...)
(?<! ...)
 
C

Cloink

Thanks for all your help.

Whilst I generally DO recommend O'Reilly books, not only is their Perl
in a Nutshell book badly written, it now appears it is quite crucially
wrong too.

I will try and remember "perldoc perlre", but I don't use Perl day in
day out, so I am likely to forget.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top