← Back to list

[SOLVED] Regex to find string arguments of a function call (lookbehind with multiple hits)

Ted James · 2025-04-24 11:49 · 0 claps · 1.3 min read
#regex #grep #regex-group #pcre #regex-greedy
Open on Medium ↗

Regex to find string arguments of a function call (lookbehind with multiple hits)

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?

Solution

You might use grep with -P for PCRE and -o to print only the matched parts.

The pattern in parts matches:

  • (?: Non capture group
  • \bfoo\( Match the word foo followed 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 \G can 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 '
  • \K Forget what is matched so far as we don't want that ' in the result
  • \w+ Match 1 or more word characters

Example:

grep -oP "(?:\bfoo\((?=[^()]*\))|\G(?!^))[^']*(?:'\h*[,:]\h*)?'\K\w+" file

See a regex demo for the matches.

An alternative using awk first matching the format foo(....) and then printing all the values between the single quotes that are alphanumeric or an underscore using a while loop.

The \047 is a single quote here.

awk '/foo\([^][]*\)/ {
  while(match($0, /\047[[:alnum:]_]+\047/)) {
    print substr($0, RSTART+1, RLENGTH-2)
    $0 = substr($0, RSTART + RLENGTH)
  }
}' file

Answered By — The fourth bird

Answer Checked By — Dawn Plyler (FixIt Volunteer)

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0


메타데이터
post_id
d617181f71ce
slug
solved-regex-to-find-string-arguments-of-a-function-call-lookbehind-with-multiple-hits-d617181f71ce
url
https://medium.com/@fixitblog/solved-regex-to-find-string-arguments-of-a-function-call-lookbehind-with-multiple-hits-d617181f71ce
canonical_url
https://medium.com/@fixitblog/solved-regex-to-find-string-arguments-of-a-function-call-lookbehind-with-multiple-hits-d617181f71ce
author_url
https://medium.com/@fixitblog
status
ok
fetched_at
2026-07-20 04:56:50