I want to use grep (PCRE) to find all single-quoted strings that are passed to my function foo().
Example functions calls in my source code and the expected hits:
foo('Alice') -> Expected Hits: Alice
foo('Alice', 'Bob', 'Charlie') -> Expected Hits: Alice, Bob, Charlie
foo(flag ? 'Alice' : 'Bob') -> Expected Hits: Alice, Bob
My regex:
foo\([^\)]*\K(?:')([^'\)]*)(?:'\))
However, I get only the last single-quoted string for each function call and not all as you can see in my regex101 playground: https://regex101.com/r/FlzDYp/1
How can I define a PCRE conform regex for grep to get all expected hits?
You might use grep with
-Pfor PCRE and-oto print only the matched parts.The pattern in parts matches:
(?:Non capture group\bfoo\(Match the wordfoofollowed by((?=[^()]*\))Positive lookahead to assert a closing)to the right|Or\G(?!^)Assert the current position at the end of the previous match, but not at the start of the string (as\Gcan match at those 2 positions))Close the non capture group[^']*match optional chars other than'(?:'\h*[,:]\h*)?Optionally match either,or:between optional spaces'Match the'\KForget what is matched so far as we don't want that'in the result\w+Match 1 or more word charactersExample:
See a regex demo for the matches.
An alternative using
awkfirst matching the formatfoo(....)and then printing all the values between the single quotes that are alphanumeric or an underscore using a while loop.The
\047is a single quote here.