views:

53

answers:

1

Hi,

I was using a regex pattern to break down the context path for a servlet.

/{1,2}([^/{1,2}]+)

This works great for simple paths like /User/folder1/folder2/folder3/.

In more real world scenario however there seems to be a problem if one of the folder names contains a dotted version number, such as: /User/username/Library/Tomcat/apache-tomcat-6.0.24.

In this case Matcher.group(1) will return apache-tomcat-6.0. instead of apache-tomcat-6.0.24. I don't know why that happens; I believe it should not.

Any insights?

Edit

This works:

/{1,2}([^/]+)
+5  A: 

[^/{1,2}] means "every character except /, {, 1, ,, 2 and }", so the 2 of 24 doesn't get matched (it will be the same with a path like a/2 and is unrelated to version numbers). Inside […], most characters are interpreted literally, and constructs such as {1,2} don't work. I think it should work if you simply say [^/]+ instead. I'm not sure why you want to match two consecutive slashes anyway—simply match a single slash and filter out empty directory names.

Philipp
Alright, thanks. The reason why I want to match double slashes is because I also want to use this regex for xpath expressions, in which `//` is an abbreviation for any descendant element.
FK82