Reputation: 882
This website contains different Url, But i want my application should vist urls only which contains specific keyword like "drugs" like if urls are
http://website.com/countryname/drug/info/A
http://website.com/countryname/Browse/Alphabet/D?cat=company
it should visit first URL.so how to match a specific keyword drug in url.I know it can be done using regexp also,but have but i am new to it I am using Java here
Upvotes: 1
Views: 1769
Reputation: 11958
You can check if string contains a word with method contains().
if(myString.contains("drugs"))
If you need only URLs containing /drug/ try to do something like this:
Pattern p = Pattern.compile("/drug(/|$)");
Matcher m = p.matcher(myURLString);
if(m.find())
{
something_to_do
}
(/|$)
means that after /drug
can be only a slash ( / ) or nothing at all (dollar means end of the line).So this regex will find all if your string is like .../drug/...
or .../drug
Upvotes: 2
Reputation: 121830
Use split()
as such:
final String[] words = input.replaceFirst("https?://", "").split("/+");
for (final String word: words)
if ("whatyouwant".equals(word))
//do what is necessary since the word matches
If your code is called very often, you may want to make Pattern
s out of https?://
and /+
and use Matcher
s.
Upvotes: 0