开发者

Java - Regex problem

开发者 https://www.devze.com 2022-12-27 01:56 出处:网络
I have a list of URLs of type http://www.example.com/pk/ca, http://www.example.com/pk开发者_运维问答,

I have a list of URLs of type

  • http://www.example.com/pk/ca,
  • http://www.example.com/pk开发者_运维问答,
  • http://www.example.com/anthingcangoeshere/pk, and
  • http://www.example.com/pkisnotnecessaryhere.

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


Your problem isn't fully defined so I can't give you an exact answer but this should be a start you can use:

^[^:]+://[^/]+\.com/pk/?$

These strings will match:

http://www.example.com/pk
http://www.example.com/pk/
https://www.example.com/pk

These strings won't match:

http://www.example.co.uk/pk
http://www.example.com/pk/ca
http://www.example.com/anthingcangoeshere/pk
http://www.example.com/pkisnotnecessaryhere


String pattern = "^http://www.example.com/pk/?$";

Hope this helps.

Some details: if you don't add ^ to the beginning of the pattern, then foobarhttp://www.example.com/pk/ will be accepted too. If you don't add $ to the end of the pattern, then http://www.exampke.com/pk/foobar will be accepted too.


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:

If you use find():

\.com/pk/?$

If you use matches():

.*\.com/pk/?

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.


String pattern = "^https?://(www\.)?.+\\.com/pk/?$";
0

精彩评论

暂无评论...
验证码 换一张
取 消