正则表达式 - 忽略捕获组中的特定字符串

时间:2022-12-01 09:57:30

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\/([^-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 个解决方案

#1


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.

观看现场演示。

#2


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

看演示

#1


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.

观看现场演示。

#2


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

看演示