Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: regex problem: doesn't work on the first search but works on the second

by Anonymous Monk
on Nov 28, 2013 at 10:06 UTC ( [id://1064785]=note: print w/replies, xml ) Need Help??


in reply to Re: regex problem: doesn't work on the first search but works on the second
in thread regex problem: doesn't work on the first search but works on the second

Thanks for the quick replies. Unfortunately, I cannot provide the exact data due to confidential agreements with my customers. But dropping the 'g' as rminner suggested did the trick. However I don't really understand the logic behind this. Could you perhaps explain why? (or link a thread, where this is explained?). Thanks a lot for the help!

  • Comment on Re^2: regex problem: doesn't work on the first search but works on the second

Replies are listed 'Best First'.
Re^3: regex problem: doesn't work on the first search but works on the second
by AnomalousMonk (Archbishop) on Nov 28, 2013 at 19:52 UTC
    ... I cannot provide the exact data due to confidential[ity] agreements ...

    No need to provide proprietary data or code. A small, standalone, working example including dummy data such as provided by tobyink would have done the trick. Indeed, the process of writing and verifying such code would probably have given you valuable insight into what was going on: tobyink's code seems to do just what you want even with needless  /g modifiers, and this should have rung a bell or two for you.

Re^3: regex problem: doesn't work on the first search but works on the second
by AnomalousMonk (Archbishop) on Nov 28, 2013 at 19:34 UTC
    Could you perhaps explain why?

    In conjunction with the SO explanations linked by rminner, consider this variation on rminner's code. The  @- special match array variable (see perlvar) is used to show the starting position of the match that is found:  $-[0]
    Then pos is printed to show the string position from which further matching will continue. The second match never finds  'foo' because when it trys to do so, it's already past it! Now remove all the  /g regex modifiers from all the matches and see what happens. See perlre, perlretut.

    >perl -wMstrict -le "my $string = 'foo bart bare'; ;; if ($string =~ m{bar}g) { print qq{found bar at offset $-[0]}; } print 'string pos is ', defined(pos $string) ? pos $string : 'UNDEF'; ;; if ($string =~ m{foo}g) { print qq{found foo at offset $-[0]}; } print 'string pos is ', defined(pos $string) ? pos $string : 'UNDEF'; ;; if ($string =~ m{bar}g) { print qq{found bar at offset $-[0]}; } print 'string pos is ', defined(pos $string) ? pos $string : 'UNDEF'; " found bar at offset 4 string pos is 7 string pos is UNDEF found bar at offset 4 string pos is 7

    In the code as shown, the second  'bar' match at offset 9 is never found because the intervening  m{foo}g match fails and 'resets' the string match position. With all  /g modifiers in place, try matching with  m{foo}gc (note the added  /c modifier) and see what happens.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found