0

My URL will look something like this :

"/eshop/products/SMART+TV+SAMSUNG+UE55H6500SLXXH+3D/productDetail/ger_20295028/"

Where product names can keep changing here SMART+TV+SAMSUNG+UE55H6500SLXXH+3D and product id here ger_20295028. I tried writing a regex which is wrong.

How can I correct it for the above URL?

Regex:

.*/products/[^/]*?/productDetail/[^/]*?/([^/].*?)/[^/]*?/([^/]*)(/.*?)*$

1
  • Welcome to Stackoverflow. It would be better if you checkout How to Ask page for future endeavor at Stack overflow.Great question tend to provide quicker, better answers from the community -Thank you Commented Mar 14, 2018 at 9:43

2 Answers 2

1

You use ? (single character) instead of * (any number) and you also have much more parts at the end than the example you've given. Try something like this

.*/products/[^/]*/productDetail/[^/]*/

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

3 Comments

Note that this allows for parts of the URL to be empty, eg /products//productDetail//
This regex is working fine but I don't want that part to be empty , how can i do that?
Change [^/]* to [^/]+ as others have suggested
0

You should read up on quantifiers (the ? means once or zero times, you are confusing it with *). This regex might work for you:

/^.*\/products\/[^\/]+\/productDetail\/[^\/]+\/$/

Try it online here.

4 Comments

No , it did not get match
@Geek you didn't specify what language you are using to match, but try this: ^.*/products/[^/]+/productDetail/[^/]+/$ instead.
I am using JAVA
"^.*/products/[^/]+/productDetail/[^/]+/$" works fine in Java.

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.