1

I have a regex with two named parts and an optional hyphen separator.

^(?<Left>xxx)-?(?<Right>yyy)$
(I've simplified the actual regex down. Instead of 'xxx' and 'yyy', imagine two really long and complicated regexes.)

However, because the hyphen is optional, there are input strings where the implied separator could be added into different places. Is there a way I can resolve the ambiguity by saying that Left or Right should take the larger share of the input string?

For example, for an input "ABCDEF" that could be split as either "ABC"/"DEF" or "ABCDE"/"F" with both being valid matches of the two sub-regexes. Say I prefer the second split because I want 'Left' to take the largest chunk it can so long as 'Right' is left with a valid remainder.

I'm using .NET's regex library, but I hope there's a standard technique.

billpg
  • 3,195
  • 3
  • 30
  • 57
  • Maybe you can use `?` to make one of the patterns non-greedy, like `\d*?` matches the least possible number of times. – hochl Feb 22 '12 at 11:06

1 Answers1

2

There is no ambiguity in regex, it will match as you designed it.

You can change the matching behaviour of your quantifiers inside each regex to change the result.

Per default they are all greedy. You can change this behaviour of matching as much as possible, to match as less as possible by adding a question mark after the quantifier e.g. .+?.

\(.*\) will match

(a)b(c)
^^^^^^^

while \(.*?\) would match

(a)b(c)
^^^ 
stema
  • 90,351
  • 20
  • 107
  • 135