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

Discussion in 'Perl Misc' started by Cloink, Oct 8, 2007.

  1. Cloink

    Cloink Guest

    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.
    Cloink, Oct 8, 2007
    #1
    1. Advertising

  2. Cloink

    Guest

    Cloink <> wrote:
    >
    > 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.
    , Oct 8, 2007
    #2
    1. Advertising

  3. Cloink

    Mumia W. Guest

    On 10/08/2007 12:41 PM, Cloink wrote:
    > 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:

    (?<= ...)
    (?<! ...)
    Mumia W., Oct 8, 2007
    #3
  4. Cloink

    Cloink Guest

    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.
    Cloink, Oct 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    File.separatorChar and regexes.

    Roedy Green, Aug 22, 2003, in forum: Java
    Replies:
    0
    Views:
    1,783
    Roedy Green
    Aug 22, 2003
  2. Jason Smith

    Does Python optimize regexes?

    Jason Smith, Jun 29, 2004, in forum: Python
    Replies:
    5
    Views:
    307
  3. Klaus Neuner
    Replies:
    7
    Views:
    472
    Klaus Neuner
    Jul 26, 2004
  4. Fernando Rodriguez

    Trouble with regexes

    Fernando Rodriguez, May 25, 2005, in forum: Python
    Replies:
    2
    Views:
    323
    Tim Roberts
    May 27, 2005
  5. Mukesh
    Replies:
    4
    Views:
    606
    Paul N
    Mar 26, 2010
Loading...

Share This Page