Christopher Penn
Christopher Penn

Reputation: 539

Golang regex for requiring multiple capture groups

I'm sure I'm missing something obvious, but I'm trying to match multiple capture groups, requiring one of each and can't see what's wrong. This is regex for Golang.

Given:

abc.com
abc.com/help
abc.com/win
abc.com/lose
abc.com/lose/wowyoulost
www.abc.com/win
www.abc.com/really/you/win
cde.com
cde.com/win

And the regex:

(abc\.com)+(win|lose)+

I am trying to require 1 or more of the first group and 1 or more of the second group. The intended outcome should be:

abc.com - NO MATCH
abc.com/help - NO MATCH
abc.com/win - MATCH
abc.com/lose - MATCH
abc.com/lose/wowyoulost - MATCH
www.abc.com/win - MATCH
www.abc.com/really/you/win - MATCH
cde.com - NO MATCH
cde.com/win - NO MATCH

What am I doing wrong?

Upvotes: 0

Views: 361

Answers (1)

Dinei
Dinei

Reputation: 5424

What you want is the lazy quantifier: .*?:

abc\.com/.*?(win|lose)

See https://regex101.com/r/STl8t7/1

Upvotes: 1

Related Questions