Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: regular expression

by choroba (Abbot)
on Aug 03, 2012 at 14:51 UTC ( #985255=note: print w/ replies, xml ) Need Help??


in reply to regular expression

You can use this regex:

/(.*)(\1.*)/
The desired string is stored in $2.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: regular expression
Download Code
Re^2: regular expression
by pileofrogs (Priest) on Aug 03, 2012 at 16:51 UTC

    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?

      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 ++!

Re^2: regular expression
by tj_thompson (Scribe) on Aug 03, 2012 at 18:33 UTC

    While this will work for this string, we're obviously dealing with a perl novice here and this solution is difficult to understand at best for someone learning the language.

    If you're ever uncertain as to why your regex is not working, put a capture '()' around each element of the regex and dump them out. You'll see exactly which regex elements are matching which pieces of your string. This will give you a lot of insight into what might be going wrong. Also be sure that you check the return value of your match is true. You might not be matching at all.

    As pointed out by many previous posters, I recommend the OP research greedy and non-greedy matches. Also read up on anchoring regexes with '^' and '$'.

Re^2: regular expression
by Rahul Gupta (Sexton) on Aug 04, 2012 at 05:31 UTC
    Thanks.It worked for me :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-08-22 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls