2

I'm trying to use the regex possibilities of Spring MVC's RequestMapping to distinguish between several REST APIs:

/somepath/{item:[^&?/]+}/{subitem:[^&?/]+}
/somepath/{item:[^&?/]+}
/somepath

In each case the API returns a totally different JSON object.

Spring MVC has the usually useful habit of conflating multiple slashes or ignoring trailing slashes. However, I need to get around that in this case. So I'm using regular expressions to handle the cases with the path variables are empty i.e. to stop /somepath//asubitem being matched to the second API rather than the first.

The problem is that when I use a slash character in the regex, the pattern will not match. Just using [^&?]+ matches but obviously this is not quite what I need.

Does anyone have an idea why we have this regex behaviour and how to get around it?

tyke
  • 71
  • 7
  • you have to escape the `/` by using another `/` so `//` would match your `/` [here](http://stackoverflow.com/questions/11769555/java-regular-expression-to-match-a-backslash-followed-by-a-quote) you will find more information from the SO-Community in the accepted answer below – Zorian Mar 26 '15 at 10:21
  • Nope, that doesn't help. The cited post talks about doubling back slashes rather than forward slashes. – tyke Mar 26 '15 at 10:55
  • the `+` quantifier is enforcing at least one character between the double slashes: use `*` – guido Mar 26 '15 at 11:49
  • Take your point but it doesn't help. It seems the the double slashes are conflated _before_ lookup: DEBUG RequestMappingHandlerMapping -- Looking up handler method for path /somepath/asubitem – tyke Mar 26 '15 at 13:25

1 Answers1

0

You can try this

/\//ig; //  Matches /

Refer following url for more detail

Forward Slash with a regex

Community
  • 1
  • 1
  • Errr, this _is_ a java question. Not a javascript question. Java doesn't use a slashy syntax for regexes. – tyke Mar 26 '15 at 13:28