0

I have this RegEx that captures a few groups:

VAN - H.Sedin (D.Sedin, A.Edler)

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$

goalscorer = H.Sedin
assist1 = D.Sedin
assist2 = A.Edler 

I want to make it so that if theres a whitespace at the end of the line, it still captures it, as sometimes there might be one at the end of the line.

I tried doing a bunch of things, most recently:

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$|\)\s+$

But I can't get it to capture groups now.

Here's a live link to test it:

https://regex101.com/r/mN8fC0/1

Paulie_D
  • 95,305
  • 9
  • 106
  • 134
rolomcflurry
  • 117
  • 8

3 Answers3

2

The problem is that your regex

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$|\)\s+$

is looking for one of two alternatives:

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)$ or \)\s+$

But the alternatives you have in mind are \)$ or \)\s+$

An additional set of parentheses constraining the alternatives will solve this problem:

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)(\)$|\)\s+$)

Alternatively, you could just use \s* instead of \s+ and an alternative, like:

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s*$

1

Here is the working regex:

^(?P<Name>\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s?$|\)

Notice the \s? after the assist1 group.

Dmitry Sadakov
  • 2,080
  • 2
  • 19
  • 31
1

Not clear about why you use |\)\s+$ instead of replace $ with \s*$. But this one is working:

^(\w+)\s-\s(?P<goalscorer>.+)\s\((?P<assist1>.+),\s(?P<assist2>.+)\)\s*$

https://regex101.com/r/mN8fC0/3

orzFly
  • 166
  • 1
  • 7