2011-12-17 54 views
1

本網站包含不同的地址,但我想我的應用程序應該訪問次數,如果網址是網址,只包含了像「毒品」一樣 特定關鍵字如何在URL匹配一個單詞(字符串)

http://website.com/countryname/drug/info/A

http://website.com/countryname/Browse/Alphabet/D?cat=company

它應該訪問第一URL.so如何匹配url.I特定關鍵字藥物知道它可以使用正則表達式也可以做,但是有,但我是新來的這 我在這裏使用Java

+0

使用字符串類的Contains()方法 – 2011-12-17 08:03:31

回答

2

您可以使用方法contains()檢查字符串是否包含單詞。
if(myString.contains("drugs"))
如果你只需要包含/藥物的URL /試着做這樣的事情:

Pattern p = Pattern.compile("/drug(/|$)"); 
    Matcher m = p.matcher(myURLString); 
    if(m.find()) 
    { 
     something_to_do 
    } 

(/|$)意味着/drug後只能在所有(美元斜槓(/)或無指結束行了)。所以這個表達式會發現所有,如果您的字符串是像.../drug/....../drug

+0

問題在於它也會匹配,比如說``藥店` – fge 2011-12-17 10:14:10

0

使用split()這樣:

final String[] words = input.replaceFirst("https?://", "").split("/+"); 
for (final String word: words) 
    if ("whatyouwant".equals(word)) 
     //do what is necessary since the word matches 

如果您的代碼經常被調用,您可能希望和/+中的Pattern s和Matcher s使用。

相關問題