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
, andhttp://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
Hope this helps.
Some details: if you don't add
^
to the beginning of the pattern, thenfoobarhttp://www.example.com/pk/
will be accepted too. If you don't add$
to the end of the pattern, thenhttp://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()
:If you use
matches()
: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.
Your problem isn't fully defined so I can't give you an exact answer but this should be a start you can use:
These strings will match:
These strings won't match: