Java - 正则表达式问题 [英] Java - Regex problem
问题描述
我有一个类型为
-
http://www.example.com/pk / ca
, -
http://www.example.com/pk
, -
http://www.example.com/anthingcangoeshere/pk
, -
http://www.example.com/pkisnotnecessaryhere
。
http://www.example.com/pk/ca
,http://www.example.com/pk
,http://www.example.com/anthingcangoeshere/pk
, andhttp://www.example.com/pkisnotnecessaryhere
.
现在,我想找到只有那些以 / pk
或 / pk /
结尾的网址,并且<$ c之间没有任何内容$ c> .com 和 / pk
Now, I want to find out only those URLs that ends with /pk
or /pk/
and don't have anything in between .com
and /pk
推荐答案
直接翻译您的请求[...]以/ pk或/ pk /结尾且在.com和/ pk之间没有任何内容的网址,并额外假设应始终存在。 com,产生这个正则表达式:
Directly translating your request "[...] URLs that ends with /pk or /pk/ and don't have anything in between .com and /pk", with the additional assumption that there shall always be a ".com", yields this regex:
如果你使用 find()
:
\.com/pk/?$
如果您使用 matches()
:
.*\.com/pk/?
此处给出的其他答案给出了更多限制性模式,只允许更接近您的示例的网址。特别是我的模式不验证给定的字符串是语法上有效的URL。
Other answers given here give more restrictive patterns, allowing only URLs that are more close to your examples. Especially my pattern does not validate that the given string is a syntactically valid URL.
这篇关于Java - 正则表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!