in reply to RE prollem: \G, \\? and disappearing data

You're getting burnt by or short-circuiting. Your code works like this.

  1. The first regex is matched against the string. At this point it matches against \$bar
  2. Because the first expression matches (i.e. returns true, the entire or is evaluated as true and there is no need to evaulate the second expression - so Perl doesn't

I think you'd be better off using alternation in your regular expressions rather than using two regular expressions.

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

  • Comment on Re: RE prollem: \G, \\? and disappearing data

Replies are listed 'Best First'.
(bbfu)(logical 'or' short-circut)Re: Re: RE prollem: \G, \\? and disappearing data
by bbfu (Curate) on Feb 12, 2001 at 14:22 UTC

    Hrm. Actually, that's what the \G is for. It anchors to the place that the last /g match matched (sorta like the ^ anchor) and defaults to the start of the string (if no previous matches were made). So, theoretically, the first RE shouldn't match at all until after the second RE has matched at least once. And that's my problem: it isn't working that way.

    Also, there are several other RE's in the actual tokenizer so alternation isn't really an option.

    I built my example more or less out of the snippet provided here under the section about the /g modifier, and the other parts of the "lex-like scanner" (as it's called in the perlop page) work just fine. The problem seems to be totally with the \\? (specifically the ? part).

    I do thank you for the suggestion though. :-)

    (PS: Just so it's clear: the idea is to match an identifier preceded by an ampersand and, optionally, a single backslash. The characters infront of the ampersanded identifier (except for the optional backslash) should be matched by the second RE, effectively breaking the string into "tokens". The actual routine is a bit more complicated and has a lot of other, unrelated code that I took out for (sanity|readibility)'s sake.)

    (PPS: Okay, so I used a dollar-sign in the code snippet. It's an ampersand in the actual routine, I promise. :-) And it doesn't matter anyway.)

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.