in reply to Re^2: regex extraction for variable number of args
in thread regex extraction for variable number of args

A regular expression without /g will always match a string from the start, and only once. You need /g for making the engine try multiple times. Your for loop only gets one result. Whether that result is the first target or the last target hinges on where your regex can match.
  • Comment on Re^3: regex extraction for variable number of args

Replies are listed 'Best First'.
Re^4: regex extraction for variable number of args
by NetWallah (Canon) on Jul 21, 2012 at 14:46 UTC
    Ok - it is beginning to sink in - so - it appears that there is no useful/meaningful way to combine non-capturing parens and repetition, unless your pattern starts at the beginning - in which case, you are better-off with the "/g" modifier . Or you are always better-off with the "/g" modifier.

    So - is there a use case for non-capturing parens with repetition ?

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      I can think of many uses for non-capturing parentheses with repetition. In fact, non-capturing parentheses with repetition are more sane than capturing parentheses with repetition, because the capturing parentheses will only keep the last match of the repetition. As an example a simple checker to check that a line matches a CSV-like format, using non-capturing parentheses and repetition:

      $line =~ /^(?:[^,]+|"[^"]+")(?:,(?:[^,]+|"[^"]+"))*$/

      That one matches one well-formed column, and then optionally a repetition of comma and more well-formed column.