简体   繁体   English

正则表达式匹配网址字符串

[英]Regex matches on Url String

I need some help in working with regex. 在使用正则表达式时,我需要一些帮助。

I have the following list of urls where I only want to match the first one. 我有以下网址列表,我只想匹配第一个。 What I'm trying to do is exclude all other links except the product url from a site which doesnt have any other slashes. 我想做的是从一个没有其他斜杠的站点中排除除产品URL之外的所有其他链接。 The Product urls pages are just "product-name.aspx" 产品网址页面只是“ product-name.aspx”

I have tried the following regex but cant quite master it, as it returns all matches. 我已经尝试了以下正则表达式,但不能完全掌握它,因为它会返回所有匹配项。

.*\.aspx

Here are examples of the urls. 这是网址的示例。 The first one is a product url I want to keep, the other should not be matched. 第一个是我要保留的产品网址,另一个不应匹配。

/1-KG-HEAT-PUMP.aspx
/Functions/ShoppingCart/?ReturnUrl=/600MM-SLIDE-OUT-RANGEHOOD-1.aspx
/Functions/ShoppingCart?ReturnUrl=%2f600MM-SLIDE-OUT-RANGEHOOD-3.aspx
/Functions/AddToCart/?AppCode=NTA4Mjk%3d&ReturnUrl=/900W-8-COOKING-FUNCTIONS-1.aspx

Any help would be greatly appreciated 任何帮助将不胜感激

Thanks Brendan 谢谢布伦丹

I would just use a negated character class that prevents any slashes except the leading one: 我只会使用一个否定的字符类,该字符类可以防止除前导斜杠以外的任何斜杠:

/[^/]*\.aspx

This means / , then any number of non-slash characters, then .aspx 这表示/ ,然后是任意数量的非斜杠字符,然后是.aspx

若要仅匹配第一个结果,请仅匹配/和.aspx之间的字母数字和破折号(-)

 ^/([\w\-]*\.aspx)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM