Home > OS >  Regexp: wildcard character with exceptions
Regexp: wildcard character with exceptions

Time:10-05

I am not very familiar with regex. I am trying to match routes in ruby. I have a situation where I have some /route/:id that can take the 'id' parameter. I would like to match the route to any string with parameters as long as there is no forward slash. So anything like /route/123 should match but anything like /route/subroute/123 should not since there is a forward slash after 'subroute'. This is my current regex pattern that matches the '/routes/' portion and allows any string to take place of the 'id' parameter: \A\/routes\/\z*. This works, but if a forward slash is present in the 'id' portion of the route, the match still succeeds. What can I do to allow any string as the 'id' as long as a forward slash is not present?

CodePudding user response:

This ended up being the pattern that worked for my case: ^\/route(\/[A-Za-z0-9\-\_]*)?$

Since it is a route, I found it better to allow only valid url characters and I use the parentheses (...)? for cases of routes that do not take parameters at all so '/route', '/route/', and '/route/abc' will all work, but '/route/abc/' will not.

CodePudding user response:

In Ruby, ^ marks the start of any line, not string, so instead of ^, you need \A. Same with $, to match a string end position, you need to use \z.

Also, to match a single path subpart (the string between two slashes here) you can use a negated character class [^\/] / [^\/]*. If you plan to restrict the chars in the subpart to alphanumeric, hyphen and underscore, you can replace [^\/] with [\w-].

So, you can also use

/\A\/route(?:\/[\w-]*)?\z/

Details:

  • \A - start of string
  • \/route - a literal /route string
  • (?:\/[\w-]*)? - an optional (due to the last ?) non-capturing group that matches an optional sequence of / and then zero or more alphanumeric, underscore (\w) or hyphen chars
  • \z - end of string.

See the Rubular demo (here, ^ and $ are used for demo purposes only since the input is a single multiline text).

  • Related