0

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?

3
  • 1
    Do you mean ^abc\.com/(win|lose)? See regex101.com/r/Gjpkl7/1. Note that your regex could not match the strings because there is no / in the pattern to match the / in the strings. Commented Apr 28, 2020 at 12:55
  • No, I'd want it to match www.abc.com as well, so no demarcation of start or ends - same for the text fragments on the back. I'll edit to clarify. Commented Apr 28, 2020 at 13:03
  • Sorry, still not clear, see ^(?:www\.)?abc\.com(?:/[^/]+)*/[^/]*(?:win|los[te]) demo (where all [^/] are replaced with [^/\n] since it is a single multiline string demo) Commented Apr 28, 2020 at 13:06

1 Answer 1

1

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

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.