user7050514
user7050514

Reputation: 13

Regex - Ignore Particular String In Capture Group

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)

Upvotes: 1

Views: 66

Answers (2)

Bohemian
Bohemian

Reputation: 425033

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.

Upvotes: 1

Sebastian Proske
Sebastian Proske

Reputation: 8413

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

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

See the demo

Upvotes: 1

Related Questions