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.
regex: MyFunction(\((?>\((?<c>)|[^()]+|\)(?<-c>))*(?(c)(?!))\))
demo
updated regex , supports PCRE.
MyFunction\((?:[^()]*(?:\([^()]*\))*[^()]*)*\)
demo2
updated again regex, supports nested function
MyFunction\(((?:[^()]++|\((?1)\))*)\)
demo3
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.(?=\)) is a positive lookahead assertion to match the right paren )
to make a pair with MyFunction(.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With