Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balanced-parentheses problem: match function parameters [duplicate]

Tags:

regex

pcre

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.

like image 743
user9219182 Avatar asked Dec 14 '25 13:12

user9219182


2 Answers

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

demo

updated regex , supports PCRE.

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

demo2

updated again regex, supports nested function

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

demo3

like image 89
Oliver Hao Avatar answered Dec 16 '25 20:12

Oliver Hao


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(.
like image 33
tshiono Avatar answered Dec 16 '25 20:12

tshiono



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!