I'm trying to write pattern to recognize formula IF form string.
e.g. string
[@param_6@] @IF(@param_5@==null;@param_5@;@IF(1==1;@param_2@;@param_4@)@)@ - @param_3@ - <@IF(@parameter_tag2@ == null;@param_4@;@param_5@)@>
I want to obtain this result
1. Match: @IF(@param_5@==null;@param_5@;@IF(1==1;@param_2@;@param_4@)@)@
2. Match: @IF(@parameter_tag2@ == null;@param_4@;@param_5@)@
My pattern look like:
@IF\(.*\;.*\;.*\)@
But regex return me wrong result.
CodePudding user response:
You can use
(?s)@IF\((?>(?!@IF\(|\)@).|(?<o>)@IF\(|(?<-o>)\)@)*\)@
See the regex demo. Details:
(?s)
-RegexOptions.Singleline
equivalent,.
now matches LF chars, too@IF\(
- a@IF(
string(?>(?!@IF\(|\)@).|(?<o>)@IF\(|(?<-o>)\)@)*
- zero or more repetitions of(?!@IF\(|\)@).|
- any char that does not start a@IF(
or)@
char sequence(?<o>)@IF\(|
- a@IF(
substring and a value is pushed on to the "o" group stack, or(?<-o>)\)@
- a)@
string and a value is popped from the "o" group stack
\)@
- a)@
string