1

I am filtering request URL in which I need to provide boolean value for regex check. I do not know whether it is possible to make such regex or I need to change URLs to follow patterns,

I got confused because I need a match for two like /cart/{orgSlug}/{cartNameSlug} & /cart/{id} where slug name must have at least one character and id must be only numeric.

I am taking care of getting the proper URL by splitting by ? & checking request method as a preliminary test.

1
  • for /cart/{id}, I have got cart\/[0-9]{1,5} Commented Aug 31, 2020 at 5:15

1 Answer 1

4

For /cart/{orgSlug}/{cartNameSlug} you may use:

/^\/cart\/[^\/]*[a-z][^\/]*\/[^\/]*[a-z][^\/]*\/?$/i

RegEx Demo

For /cart/{id}, you may use:

/^\/cart\/\d+\/?$/i
Sign up to request clarification or add additional context in comments.

4 Comments

just incredible :) I removed / at the end only.
You almost always want \A and \z instead of ^ and $ (respectively) in Ruby.
@anubhava what I can do if I want to avoid slug name should not start with a number?
@ray: Just add (?1\d) right after matching \/ to make it: ^\/cart\/(?!\d)[^\/]*[a-z][^\/]*\/(?!\d)[^\/]*[a-z][^\/]*$

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.