users@jsr311.java.net

Re: pathParam regex

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 02 Dec 2008 11:10:37 -0500

On Dec 2, 2008, at 10:43 AM, Manuel Innerhofer wrote:
>
> I'm not sure how the following cases should be handled:
>
> @Path(value="{A:[\\w]*}{B:[\\w]*}")
> What values should be assigned to the parameters with a request URI
> like "abcdef":
>
> A = "abcdef", B = "" or
> A = "", B = "abcdef" or
> A = "abc", B ="def"? Or something else?
>
Here's how to work out the answer. Follow section 3.7.3 of the spec to
convert the URI template into a regex. In the example above the result
is "([\\w]*)([\\w]*)(/.*)?". Now you can just apply the resulting
regex to the request URI:

bash$ jython
Jython 2.2.1 on java1.5.0_16
Type "copyright", "credits" or "license" for more information.
>>> import java.util.regex.Pattern as Pattern
>>> p = Pattern.compile("([\\w]*)([\\w]*)(/.*)?")
>>> m = p.matcher("abcdef")
>>> m.groupCount()
3
>>> m.group(1)
'abcdef'
>>> m.group(2)
''
>>> m.group(3)
>>>

So A which is the name of the forst capturing group is "abcdef" and B
is "".

> or in this case:
>
> @Path(value="{A:r[\\w]*d}{B:r[\\w]*d}")
> request URI "rudridred"
>
> A= "rud", B="ridred"
> A= "rudrid", B="red"

The regex is "(r[\\w]*d)(r[\\w]*d)(/.*)?":

>>> p = Pattern.compile("(r[\\w]*d)(r[\\w]*d)(/.*)?")
>>> m = p.matcher("rudridred")
>>> m.groupCount()
3
>>> m.matches()
1
>>> m.group(1)
'rudrid'
>>> m.group(2)
'red'
>>> m.group(3)
>>>

So A is "rudrid" and B is "red".

Hope that helps,
Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.