I am trying to create a regex that will parse a string that is of either one of the two forms:
name (tag) @ name (tag)
name @ name
* name and tag can be multiple words separated by spaces
The regex I wrote for this is:
(?<a>.+?)(\s\(.+\))?\s@\s(?<b>.+?)(\s\(.+\))?
This fails when parsing a string of the first form which includes tags surrounded by parentheses. Specifically, it will only match one character in the 2nd name, essentially ignoring the 2nd tag group. It seems as though the greediness of the ? quantifier for the 2nd tag group is ignored, or of lower priority than the lazy quantifier of the group.
Furthermore, if I add an end of string anchor, $, to the end of the pattern, it will put everything on the left side of @, including the tags, into the a group and everything to the right side into the b group, which again seems unexpected.
Why is this happening? Is there a way to account for the two groups without branching?