1

I'm looking to capture the string between www.my-website.com/buy/ and -PERMANENT.html if it exists, but am having trouble doing so. I'm unsure how to create a capture group that would ignore a consecutive string. My attempts have failed so far.

www\.my-website\.com\/buy\/([^-PERMANENT.html]*)

www.my-website.com/buy/fork                          (Capture fork)
www.my-website.com/buy/wand                          (Capture wand)
www.my-website.com/buy/ball-PERMANENT.html           (Capture ball)
www.my-website.com/buy/bike-PERMANENT.html           (Capture bike)
www.my-website.com/buy/base-PERMANENT.html-ball      (Capture base)
www.my-website.com/buy/wall-PERMANENT.html-glue      (Capture wall)

2 Answers 2

1

You don't even need groups. The whole match of this regex is your target:

(?<=www\.my-website\.com\/buy\/).*?(?=-PERMANENT.html|$)

See live demo.

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

Comments

1

If your URLs are really that simple, you could go by lazy matching, like

www\.my-website\.com\/buy\/(.*?)(?:-PERMANENT.html|$)

See the demo

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.