Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: regular expression

by pileofrogs (Priest)
on Aug 03, 2012 at 16:51 UTC ( #985286=note: print w/replies, xml ) Need Help??


in reply to Re: regular expression
in thread regular expression

Woah!

That works. I expected it would fail because the first group would match everything and then there would be nothing for the 2nd group to match against.

Here's the code I used to test:

#! /usr/bin/perl -w -T use strict; my $str = 'foo_bar_foo_bar_12345'; print "$str\n"; $str =~ /(.*)(\1.*)/ || die "Failed!\n"; print "$2\n";

Can anyone explaine why that is?

Replies are listed 'Best First'.
Re^3: regular expression
by Athanasius (Chancellor) on Aug 03, 2012 at 17:21 UTC

    From Backtracking:

    For a regular expression to match, the entire regular expression must match, not just part of it. So if the beginning of a pattern containing a quantifier succeeds in a way that causes later parts in the pattern to fail, the matching engine backs up and recalculates the beginning part—that's why it's called backtracking.

    The regex engine begins as you say, by matching everything to the first .*, but when the whole match fails it then backtracks one character and tries again. Eventually, it has backtracked to the point at which $1 contains foo_bar and $2 contains foo_bar_12345. The regex engine then verifies that this value of $2 does finally satisfy the condition \1.*, so the entire match succeeds and the regex engine stops looking and returns.

    HTH,

    Athanasius <°(((><contra mundum

      Big ++!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985286]
help
Chatterbox?
[stonecolddevin]: erix thanks mate
[stonecolddevin]: also lol @ "I stomped a crap"
[stonecolddevin]: Disciplus i was just amazed he's returned after the thorough stomp that BrowserUk and several other monks gave him for spreading crap info
[stonecolddevin]: i will admit i find the responses to his threads incredibly entertaining
[stonecolddevin]: erix what is this wizardy? there's no sound for me :-(
[stevieb]: he comes and goes every few years.
[erix]: stonecolddevin: oh? Don't know, it works for me
[Sinistral]: stonecolddevin / stevieb - would 'he' in this context (looked at last 50 lines and didn't see discussion) be a certain solar timepiece user?
[stonecolddevin]: Sinistral yes indeed

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-06-22 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (529 votes). Check out past polls.