Regex does not properly break down file path with a version number
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.开发者_如何学C0.24
. I don't know why that happens; I believe it should not.
Any insights?
Edit
This works:
/{1,2}([^/]+)
[^/{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.
精彩评论