简体   繁体   中英

regular expressions wrapped inside {}

Am little stuck and wondering if anyone can help, am trying to use regular expressions to find value & check Funtion2 is between the {} in the following string see below:

AA \\*Funtion1 {5 + \\*Funtion2 {3} {4} + 6 } BB 

CC \\*Funtion2 {3} {\\*Funtion2 {3} {4} + 4} DD \\*Funtion2 {3} {4} EE

AA \\*Funtion1 { \\*Funtion2 {3} {4} + \\*Funtion2 {3} {4} + 6 } BB

Should return 2 matchs but keep getting 3.

Try using a lookbehind.

(?<=\{[^}]*)Funtion2

This will find 'Funtion2' that is preceded by '{' but does not have a '}' between the opening brace and the text.

Note, however, that this does not balance opening and closing braces. From your example text, I don't see this being a problem.

This will fail to find all matches if the following scenario occurred:

AA \\*Funtion1 { \\*Funtion2 {3} {4} + \\*Funtion2 {3} {4} + 6 } BB 

The second 'Funtion2' would be skipped because there's a '}' between it and the opening '{'.

You could use a balanced regex, but honestly this looks like parsing to me. Perhaps you should consider writing a parser and not relying so heavily on regexes.

Will there ever be braces within braces, as in {3 + { whatever } } ? Will there ever be backslashes that are not part of a function name (eg \\\\*Funtion2 )? If the answer to both questions is no, you should be able to manage this without resorting to balanced groups. For example:

Regex r = new Regex(@"\{[^{}\\]*\\\\\*Funtion2(?:[^{}\\]+\{[^{}\\]+\})*[^{}\\]*\}");
foreach (Match m in r.Matches(source)
{
  Console.WriteLine(m.Value);
}

result:

{5 + \\*Funtion2 {3} {4} + 6 }
{\\*Funtion2 {3} {4} + 4}

Breaking the regex down, we have:

\{              # the opening brace
[^{}\\]*        # optional stuff preceding the function name
\\\\            # the two backslashes
\*              # the asterisk
Funtion2        # and the name
(?:             # in a loop...
  [^{}\\]+        # stuff preceding the next opening brace
  \{[^{}\\]+\}    # a balanced pair of braces with non-braces in between
)*              # loop zero or more times
[^{}\\]*        # optional stuff preceding the closing brace
\}              # the closing brace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM