WeepingBoil
WeepingBoil

Reputation: 73

Regex - is this nesting or recursion - or even possible?

Can anyone help me with a regex expression to solve this:

[x: {aaa}] blablabla [x: {bbb}, {ccc}] blablabla [y: {ddd}]

required: aaa bbb ccc

i.e. look for [x: ] then get the text in curly brackets - and ignore anything in [y: ]

Is it even possible?

Upvotes: 1

Views: 44

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

In C#, you can use a non-regex approach (if your data is always well-formatted):

var s = "[x: {aaa}] blablabla [x: {bbb}, {ccc}] blablabla [y: {ddd}]";
var res = s.Split('[', ']')          // Split with `[` and `]`
   .Where(q => q.StartsWith("x: "))  // Only get those elements starting with x:
   .SelectMany(m => m.Trim().Substring(4, m.Length - 5).Split(new[] { "}, {" }, StringSplitOptions.None))
   .ToList();                        // Convert to a List<string> all the elements

enter image description here

A regex is preferable if the data is not always well-formatted. Here is one you can use:

@"\[x:\s*{(?<val>[^}]*)}(?:\s*,\s*{(?<val>[^}]*)})*"

Breakdown:

  • \[x:\s* - a sequence of characters [x: followed with 0+ whitespace(s)
  • {(?<val>[^}]*)} - a {...} substring the inner part of which is captured into a named capture group named "val" (it has a stack for all the values captured accessible via Match.Groups["val"].Captures)
  • (?:\s*,\s*{(?<val>[^}]*)})* - 0+ sequences of:
    • \s*,\s* - 0+ whitespaces followed with a comma and again 0+ whitespaces
    • {(?<val>[^}]*)} - same as above, adding more values to the "val" group stack.

NOTE that "val" cannot contain } in this case. See the regex demo, just grab the match.Groups["val"].Captures.

var s = "[x: {aaa}] blablabla [x: {bbb}, {ccc}] blablabla [y: {ddd}]";
var res = Regex.Matches(s, @"\[x:\s*{(?<val>[^}]*)}(?:\s*,\s*{(?<val>[^}]*)})*")
        .Cast<Match>()
        .SelectMany(p => p.Groups["val"].Captures.Cast<Capture>().Select(m => m.Value))
        .ToList();

enter image description here

Upvotes: 2

Related Questions