Regex find matches from a string containing multiple square brackets

I have a paragraph as shown below:

Some wording for testing [! #today], where the condition [!] does not satisfy this wording [! ShowElemIf: // Student / FullName; [[[Text is not fully recognized]]]; / Name] But simple tags with age [! ShowElemIf: // Student / Age; xml // Student / DOB / @ formatted; y]

I need to find all the tags / tags on top that are: [! tag] use C #. I tried to create a regular expression, but could not find a tag that has the word "FullName" in bold.

List<string> tags = Regex.Matches(
                     sampleText.Replace(Environment.NewLine, ""), @"\[!([^]]+)\]	")
                     .Cast<Match>()
                     .Select(x => x.Groups[1].Value)
                     .ToList();
Run codeHide result

Using this RegEx, I can find below, but not highlighted.

  • Today
  • state
  • ShowElemIf: // Student / Age, XML // Student / DOB / @ formatted, y
+4
1

, .NET regex:

@"\[!((?:[^][]+|(?<o>\[)|(?<-o>)])*(?(o)(?!)))]"

regex

  • \[! - [!
  • ((?:[^][]+|(?<o>\[)|(?<-o>)])*(?(o)(?!))) - 1
    • (?: - :
      • [^][]+| - 1 , [ ],
      • (?<o>\[)| - a [, "o" ,
      • (?<-o>)] - a ] "o"
    • )* -
    • (?(o)(?!))) - , , "o" . , , .
  • ] - ].
+3

Source: https://habr.com/ru/post/1673709/


All Articles