Search code examples
regexstring-matching

How to match a function but exclude object methods without negative lookbehind


I'm trying to write a regex that matches every occurrence of some_function(...), but it should not match when it's part of an object method like my.some_function(...) or if it is a substring of another function, i.e., myname_some_function(...). The function can appear in various contexts, including within another function call, after whitespace, or standalone:

Cases that should match:

some_function(...)
other_function(some_function(...))
(some_function(...))
, some_function(...)
,some_function(...)
\t some_function(...)
\n some_function(...)

Cases that should not match:

myname_some_function(...)
object.some_function(...)

Constraints: I cannot use negative lookbehind ((?<!.)) since the environment does not support it. The some_function should not be preceded by a dot (.) or an alphanumeric character.

What I've tried so far:

'[^.]\\s*some_function\\s*\\(.*'

This works in some cases but doesn't handle when some_function appears after parentheses, e.g., other_function(some_function(...)).

'[^.\\w]\\s*some_function\\s*\\(.*'

This excludes object methods like my.some_function(), but fails when some_function appears right after an opening parenthesis or comma (e.g., , some_function(...), (some_function(...))).


Solution

  • You can use a non-capturing group and the OR operator | to match either:

    • A character other than one of the negated set [^...]
    • Exceptions to the negated set if you want to allow some part of sets like \d or \w
    • The beginning of a line ^

    You may need to set the multiline flag /m so that matching the beginning of the line works consistently

    /(?:[^\.\_\w\d]|[\(\;\)]|^)some_function/m
    

    You can see that it matches your exampless here: regExr link