in reply to Re: String order in regex match - left to right, or right to left?
in thread String order in regex match - left to right, or right to left?

Enlil stated "The difference is that you have to remember that in:
my $ua_name = $1 if ($ua=~ m/(opera|netscape|gecko|msie)/i);

The whole regex is checked at every position in your string before moving on to the next position."

I would also follow this train of thought but this actually does not explain what is happening. If this was happening Netscape would be found first, going left to right.

Does it instead go right to left or is the comparison Alex posted different than what he is running, or is it something completely different?

Thanks
  • Comment on Re: (2) String order in regex match - left to right, or right to left?

Replies are listed 'Best First'.
Re: (3) String order in regex match - left to right, or right to left?
by kelan (Deacon) on Jun 07, 2003 at 03:49 UTC

    Gecko comes first in the string that is being searched. When the regex engine is trying to match a regex in a string, it starts at the first character of the string and tries to match from the beginning of the regex. If it can't match at that position, it moves to the next character of the string and starts trying to match again (unless you anchor the regex). In this case, the match keeps failing until it gets to the point in the string where 'Gecko' appears. At that point, the regex engine says to itself: "Can this match 'opera'? No. Can this match 'netscape'? No. Can this match 'gecko'? Yes. Return captured string 'gecko'. Done." Which happens before it gets to the point in the string where 'Netscape' appears.

    kelan


    Perl6 Grammar Student

      Thanks for clearing that up!

      So...

      It takes the first word of the string ("Mozilla" in this case) and compares it to each regex, opera then netscape then gecko ...

      This is reversed as to how I interprited it. I guess I think about it backwards, as taking opera and comparing that to each word in the string.
      Kelan

      Thank you very much, that does indeed explain it. I think Enlil tried to explain this to me as well, but I didn't get it. Thank you both for your help!

      Alexander Garcia
Re: (3) String order in regex match - left to right, or right to left?
by fglock (Vicar) on Jun 07, 2003 at 03:47 UTC

    I also have problems with this left/right thing :)

    left right | | Gecko/20011019 Netscape6/6.2