Andrei Ivanov
Andrei Ivanov

Reputation: 115

regex for nested parentheses

Brief

I have the following text:

[atdSd[asdasd<4REGEH22>asdosy] ***oopprefs[ew<16REGEH30>rdtr]pppp555b

My matches should be as follows:

  1. [asdasd<4REGEH22>asdosy]
  2. [ew<16REGEH30>rdtr]

My attempts

I have tried to make it on my own but the result was:

  1. [atdSd[asdasd<4REGEH22>asdosy]
  2. [ew<16REGEH30>rdtr]

I am using the following expression:

\[\S+<(\d+)REGEH(\d+)>\S+\]

Specifics

The conditions are:

An example that should yield no matches

[atdSd[<4REGEH22>asdosy] ***oopprefs[ew<16REGEH>rdtr]pppp555b

Question

How can I match the inner parentheses?

Upvotes: 1

Views: 160

Answers (2)

LetzerWille
LetzerWille

Reputation: 5658

  var st = "[atdSd[asdasd<4REGEH22>asdosy] ***oopprefs[ew<16REGEH30>rdtr]pppp555b";

        List<string> result = new List<string>
                            (Regex.Matches(st, @"\[[^[]+REGEH.*?\]")
                            .Cast<Match>()
                            .Select(x => x.Value)
                            .ToList());

        // [asdasd<4REGEH22>asdosy] [ew<16REGEH30>rdtr]

Upvotes: 1

Bananaapple
Bananaapple

Reputation: 3114

How about this?

\[[^\s\[]+<(\d+)REGEH(\d+)>[[^\s\[]+\]

Basically replaced the \S with [^\s\[], ie a negated char class matching for not whitepsace chars and, crucially - for not [.

http://regexr.com/3glld

Upvotes: 0

Related Questions