user1517598
user1517598

Reputation: 111

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. The Product urls pages are just "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

Upvotes: 0

Views: 93

Answers (2)

Bohemian
Bohemian

Reputation: 425003

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

Upvotes: 1

Ken
Ken

Reputation: 336

To only match the first result, match on only alpha numeric and dash (-) between / and .aspx

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

Upvotes: 0

Related Questions