I am trying to write a simple regex to match the following:
我正在尝试编写一个简单的正则表达式来匹配以下内容:
/path/foo.html
/path/foo.html?a=b
/path/foo.html?a=b&b=c
but not /path/foo.htmlx or anything else which is not foo.html + url parameter.
但不是/path/foo.htmlx或其他不是foo.html + url参数的东西。
I tried
/path/foo.html(?:\?|$)
but it does not seem to work in my java project.
但它似乎不适用于我的java项目。
3 个解决方案
#1
1
String.matches(String regex)
does a full match
String.matches(String regex)完全匹配
Tells whether or not this string matches the given regular expression.
判断此字符串是否与给定的正则表达式匹配。
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression
调用str.matches(regex)形式的此方法会产生与表达式完全相同的结果
So for example "/path/foo.html?a=b".matches("/path/foo.html(?:\\?|$)")
returns false, because the String doesn't end after the ?
.
因此,例如“/path/foo.html?a=b".matches("/path/foo.html(?:\\?|$)”)返回false,因为String不会在?之后结束。
You can use "/path/foo.html(\\?.*)?"
你可以使用“/path/foo.html(\\?.*)?”
#2
1
For URL part use regexp provided by @reconnect:
对于URL部分,请使用@reconnect提供的regexp:
^[^?]*
match from start until ?
character meet.
比赛从开始直到?性格相遇。
for search exactly /path/foo.html use:
准确搜索/path/foo.html使用:
^/path/foo.html
if you need check that part of something exists with matches in the line add ^
- start of line and $
end of line, and in this case you should care about characters between end of your conditions, basically replaced with .*
如果你需要检查某行的某些部分是否存在匹配行^ - 行和行的结尾,在这种情况下你应该关心条件结束之间的字符,基本上用。*替换。*
#3
0
I'm not exactly understand what you trying to match, but if you want match only URL part without search string:
我并不完全理解您要匹配的内容,但如果您只想匹配没有搜索字符串的URL部分:
/^[^?#]*/
# - hash string, too can pass some arguments
# - hash string,也可以传递一些参数
#1
1
String.matches(String regex)
does a full match
String.matches(String regex)完全匹配
Tells whether or not this string matches the given regular expression.
判断此字符串是否与给定的正则表达式匹配。
An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression
调用str.matches(regex)形式的此方法会产生与表达式完全相同的结果
So for example "/path/foo.html?a=b".matches("/path/foo.html(?:\\?|$)")
returns false, because the String doesn't end after the ?
.
因此,例如“/path/foo.html?a=b".matches("/path/foo.html(?:\\?|$)”)返回false,因为String不会在?之后结束。
You can use "/path/foo.html(\\?.*)?"
你可以使用“/path/foo.html(\\?.*)?”
#2
1
For URL part use regexp provided by @reconnect:
对于URL部分,请使用@reconnect提供的regexp:
^[^?]*
match from start until ?
character meet.
比赛从开始直到?性格相遇。
for search exactly /path/foo.html use:
准确搜索/path/foo.html使用:
^/path/foo.html
if you need check that part of something exists with matches in the line add ^
- start of line and $
end of line, and in this case you should care about characters between end of your conditions, basically replaced with .*
如果你需要检查某行的某些部分是否存在匹配行^ - 行和行的结尾,在这种情况下你应该关心条件结束之间的字符,基本上用。*替换。*
#3
0
I'm not exactly understand what you trying to match, but if you want match only URL part without search string:
我并不完全理解您要匹配的内容,但如果您只想匹配没有搜索字符串的URL部分:
/^[^?#]*/
# - hash string, too can pass some arguments
# - hash string,也可以传递一些参数