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

Does it work if you drop the /g at the end of your regex?
Some food for thought:
use strict; use warnings; my $string = 'foo bar'; if ($string =~ m{bar}g) { print "bar\n"; } if ($string =~ m{foo}g) { print "foo\n"; } if ($string =~ m{bar}g) { print "bar\n"; }
output:
bar bar
without the /g it will print:
bar foo bar
The /g will keep the current position in the string and as such you are later continuing your matching from that position. If you just want to match your entire string (as it seems in this case), simply try dropping the /g.
As tobyink said, it would be easier if you provided some input data.

Update:
A stackoverflow link which explains it: http://stackoverflow.com/questions/6969208/help-understanding-global-flag-in-perl

Replies are listed 'Best First'.
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

    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!

      ... 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.

      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.