tags:

views:

95

answers:

4

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

+1  A: 

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
Mark Byers
+1  A: 
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.

Bytecode Ninja
A: 
String pattern = "^https?://(www\.)?.+\\.com/pk/?$";
Martijn Courteaux
This matches `"http://files.net/command.com/pk"`
polygenelubricants
+1  A: 

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.

Christian Semrau