开发者

Java - Regex problem [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Java - Regex problem

I have list of URLs of types:

  • http://www.example.com/pk/etc
  • http://www.example.com/pk/etc/
  • http://www.example.com/pk/etc/etc

where etc can be anything.

So I want to search only those URLs that contains www.example.com/pk/etc or www.example.com/pk/etc/.

Note: It is for all those who think that it is a duplicate question -- Kindly read both the questions 开发者_如何学运维carefully before marking this question as duplicate. Even after reading you can't understand that both the questions are different, then kindly leave without marking it as duplicate because I can't tell you the diff. in anymore detail


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

I am assuming http://www.example.com/pk// is not accepted. If this should be accepted too, then use

String pattern = "http://www.example.com/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/[^/]+/?$

The difference is that the / is no longer optional and there must be at least one more character after pk/.

These strings will match:

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

These strings won't match:

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


So I want to search only those URLs that contains www.example.com/pk/etc or www.example.com/pk/etc/.

Update

I think this will work:

https?://.*\\.?[A-Za-z0-9]+\\.com/pk/etc/?[^.]

But every item in the list you gave contains what you are searching for.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜