0

Need to match "key(...)" or "key (...)" but only capture "...":

key(abc) //abc captured
key (def) //def captured

But non-fixed length look behind is forbidden, so use another non-capture group after look behind this:

(?<=key)(?:[ ]{0,1}\().+?(?=\))

But space and ( before "..." is still captured too. Doesn't non-capture mean "not captured"?

So I tried this most simple case about non capturing group:

(?:abc)def

still capture the whole abcdef, on reg101 and in C#.

The reason may be some complex concepts called captures and groups, but after try:

Regex r = new Regex("(?:abc)def");
foreach(var c in r.Match("abcdef").Captures)...
foreach(var g in r.Match("abcdef").Groups)...
foreach(var c in r.Match("abcdef").Groups[0].Captures)...

still only get abcdef...

jw_
  • 1,663
  • 18
  • 32
  • 1
    .NET supports infinite length lookbehinds, but you have to use another regex tester http://regexstorm.net/tester – The fourth bird Mar 01 '20 at 11:22
  • 3
    Am I missing something? Why not just do something like `key ?\(([^)]*)\)`? – Sweeper Mar 01 '20 at 11:23
  • @Thefourthbird That solve the original need. What about the question? – jw_ Mar 01 '20 at 11:26
  • @jw_ The non capturing group means that it is not capturing it in a group, but it still matches. See [What is a non-capturing group in regular expressions?](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions) and see [What's the difference between “groups” and “captures” in .NET regular expressions?](https://stackoverflow.com/questions/3320823/whats-the-difference-between-groups-and-captures-in-net-regular-expression) – The fourth bird Mar 01 '20 at 11:30
  • It should be (?:abc)(def). But this still give multi group to select in. – jw_ Mar 01 '20 at 11:35
  • @Sweeper have to learn capturing to use that, now I only tried non-capturing staff. – jw_ Mar 01 '20 at 11:38

0 Answers0