3

我有一个正则表达式来匹配任何 ebay 网站:

(?i)^((http|https)://)?(\w+\.)*ebay.(\w+.)+

我想否定它,匹配所有,除了这个网站

我试过了:

(?! pattern )
(?!pattern)
[^pattern]

但没有任何效果。也许我的第一个正则表达式太不理想了?

java中的测试用例:

Pattern queryPattern = Pattern.compile("(?i)^((http|https)://)?(\\w+\\.)*ebay.(\\w+.)+");
List<String> queries = new LinkedList();
queries.add("http://www.ebay.com/blabla");
queries.add("www.ebay.com/blabla");
queries.add("www.eBay.com/blabla");
queries.add("www.ebay.com.hk/blabla");
queries.add("www.ebay.co.uk");
queries.add("ttt.ebay.com");
queries.add("ru.ebay.com");
queries.add("test.ru.ebay.com");
queries.add("test.ru.ebAY.com");
queries.add("ebay.com");
queries.add("naebay.com");
queries.add("ru.naebay.ru");
queries.add("blah.ru");
for (String query : queries) {
  System.out.println(query + " " + queryPattern.matcher(query).find());
}
4

2 回答 2

6

明显的反应:

    Pattern queryPattern = Pattern.compile("(?i)^((http|https)://)?(\\w+\\.)*ebay.(\\w+.)+");
    List<String> queries = new LinkedList<String>();
    ...

    for (String query : queries) {
        if (queryPattern.matcher(query).find() == false) {
            System.out.println("Non Ebay site: " + query + " " + queryPattern.matcher(query).find());
        }
    }

输出:

Non Ebay site: naebay.com false
Non Ebay site: ru.naebay.ru false
Non Ebay site: blah.ru false
于 2013-05-14T09:29:54.713 回答
1

Avoid regex to parse irregular patterns like url,html.Use URL class.

To solve your problem You can use (?!.*ebay) at the start of your pattern i.e after ^


Instead of parsing with regex you could use URL class like this

URL url = new URL(yourURL);
if(!url.getHost().contains("ebay")){}
于 2013-05-14T09:26:10.163 回答