3

I need a PCRE regex to match a particular balanced-parentheses situation, and while I've looked at a bunch of discussions of the problem (e.g. here), I can't get anything to work. I'm trying to use regex101.com to debug.

I'm trying to match a string like foo inside MyFunction(foo), where foo can be anything, including other function calls that can themselves contain parentheses. That is, I want to be able to match

MyFunction(23)
MyFunction('Sekret')
MyFunction( 23, 'Sekret', Date() )
MyFunction( Encrypt('Sekret'), Date() )

But MyFunction can be part of a larger string which itself can have parentheses, e.g.

logger.info("MyFunction( Encrypt('Sekret'), Date() )")    

in which case I want only what is passed to MyFunction to be matched, not what is passed to logger.info.

The purpose of this is to replace whatever is passed to MyFunction with something else, e.g. "XXXXXX".

Using a regex from the linked question (e.g. \((?:[^)(]+|(?R))*+\)) will match the outermost set of parens, but if I try to preface it with MyFunctionβ€”i.e. if I try the regex MyFunction\((?:[^)(]+|(?R))*+\)β€”it fails.

user9219182
  • 329
  • 1
  • 9

2 Answers2

3

regex: MyFunction(\((?>\((?<c>)|[^()]+|\)(?<-c>))*(?(c)(?!))\))

demo

updated regex , supports PCRE.

MyFunction\((?:[^()]*(?:\([^()]*\))*[^()]*)*\)

demo2

updated again regex, supports nested function

MyFunction\(((?:[^()]++|\((?1)\))*)\)

demo3

Oliver Hao
  • 715
  • 3
  • 5
  • Yikes, I can't even begin to grok that! Unfortunately, while it works for your demo, it's not a PCRE regex as requested, so it fails for my use case. – user9219182 Feb 15 '23 at 03:05
  • Nice approach, but it will fail for a nested function call such as `MyFunction(foo(bar(baz)))`. – tshiono Feb 15 '23 at 05:02
3

Would you please try:

(?<=MyFunction\()([^()]+|\((?1)*\))+(?=\))

Demo

  • (?<=MyFunction\() is a positive lookbehind assertion to match the string MyFunction( without including the match in the capture group.
  • ([^()]+|\((?1)*\)) is the alternation of [^()]+ or \((?1)*\). The former matches a sequence of characters other than ( and ). The latter recursively matches a parenthesized string, where (?1) is the partial recursion of the capture group 1.
  • The final (?=\)) is a positive lookahead assertion to match the right paren ) to make a pair with MyFunction(.
tshiono
  • 21,248
  • 2
  • 14
  • 22