Regex Matching Negation to Ebay Sites

I have a regex to match any ebay sites:

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

I want to negate it, to match all, except this sites

I tried:

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

But nothing worked. Maybe my first regex is too unoptimal?

Testcase in java:

Pattern queryPattern = Pattern.compile("(?i)^((http|https)://)?(\\w+\\.)*ebay.(\\w+.)+");
List<String> queries = new LinkedList();
queries.add("");
queries.add("");
queries.add("");
queries.add("");
queries.add("");
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());
}
2

2 Answers

Obvious response:

    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());
        }
    }

Output:

Non Ebay site: naebay.com false
Non Ebay site: ru.naebay.ru false
Non Ebay site: blah.ru false

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")){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest