in reply to Re: Multiple if statements matching part of one variable problem
in thread Multiple if statements matching part of one variable problem

Ah, ok, so thats a "feature" of perls regex engine. So basically that code is doing to same thing as me just duplicating the $gw var and then matching against those newly created variables. Maybe its my missunderstanding of "g" but I was thinking that meant globally match anywhere in the string. So if I was wanting to match "def" in the string of "abcdefg" then I would use the "g" otherwise I'd have to match on something like /.*def.*/ Is that incorrect?
  • Comment on Re^2: Multiple if statements matching part of one variable problem

Replies are listed 'Best First'.
Re^3: Multiple if statements matching part of one variable problem
by ikegami (Patriarch) on Sep 10, 2008 at 14:20 UTC

    Yes, that's completely incorrect.

    First, "g" doesn't do that at all. It means (more or less) "all instances".

    # Check for a match if (/pat/) { ... } # Find all matches while (/pat/g) { ... }

    Second, that's not how regexps match at all.

    print( 'abc' =~ /b/ ?1:0,"\n"); # 1 print( 'abc' =~ /^b\z/ ?1:0,"\n"); # 0 print( 'abc' =~ /^abc\z/ ?1:0,"\n"); # 1 print( 'a2b3c' =~ /(\d)/ ?$1:0,"\n"); # 2 print( 'a2b3c' =~ /^.*(\d)/ ?$1:0,"\n"); # 3 print( 'a2b3c' =~ /^.*?(\d)/ ?$1:0,"\n"); # 2

    References:

Re^3: Multiple if statements matching part of one variable problem
by GrandFather (Saint) on Sep 10, 2008 at 14:35 UTC

    Yes, that is misunderstanding the role of the g modifier. Simplistically /g means "match as many times as you can". In a list context that means the regex will return all the matches it finds. Consider:

    my @matches = '1 foo 22 bar 3' =~ /\d+/g; print "@matches";

    Prints:

    1 22 3

    In scalar context however it returns true while there is a "next" match. To see what was matched we now have to capture the bit we are interested in:

    while (my $match = '1 foo 22 bar 3' =~ /(\d+)/g) { print "$1 "; }

    which generates the same output as above. Your code is rather like this last version except that you have "unwound" the loop.

    To get the behaviour you expected without the /g you need to "anchor" the match at the start of the string using ^:

    my @matches = '1 foo 22 bar 3' =~ /^\d+/g; print "@matches";

    which prints '1'. For further regex reading see perlretut, perlre and perlreref.


    Perl reduces RSI - it saves typing