Using C# Grouping Constructs in Regular Expressions one can match the content inside nested parentheses as shown by this response. Following code correctly returns (b/(2c))
and (abc)
:
st = "Test(b/(2c)) some (abc) test.";
foreach (Match mt in Regex.Matches(st, @"\((?>\((?<DEPTH>)|\)(?<-DEPTH>)|.?)*(?(DEPTH)(?!))\)"))
{
Console.WriteLine(mt.Value);
}
However, when I change the pattern to @"(?<=/)\((?>\((?<DEPTH>)|\)(?<-DEPTH>)|.?)*(?(DEPTH)(?!))\)"
by just adding (?<=/)
before the above pattern to get only the parentheses that are preceded by /
I expected to get only (2c)
but I am getting (2c))
with an extra )
. What I'm missing? Note: If my input string is Test(b)/(2c) some (abc) test.
then my new pattern correctly returns (2c)
only.