Java Regex在任何位置都有一个匹配和两个否定

时间:2016-01-27 03:53:49

标签: java regex regex-negation case-insensitive

我必须匹配一个关键字,前提是它不是复合URL的句子,也不是某些单词。例如,使用关键字 .NET ,字符串不得包含 http:// .NET 后的字符不得为< strong>工作或 flix ,但可以是框架或任何其他字词,甚至没有。正则表达式必须不区分大小写。

我有这些例子来匹配:

  • 框架 .NET
  • 那是 .NET 框架
  • 微软的 .NET
  • .NET 框架(更新)
  • .net 框架(更新)
  • .net (更新)

这些例子不匹配:

  • 这是一个网址http://www.my .net /与
  • 不匹配
  • 网络不匹配,因为缺少点
  • .NET 工作正常
  • 微软的 workAndSharingCenter
  • 4df9e0f8的达网络 flix_mcm4njqhnhss8
  • .net 工作(更新)
  • .Net 工作(更新)

我写过这种模式:

(?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)

我已经对这些测试用例进行了编码,但是testMatch_02()testNotMatch_01()都失败了,我无法弄清楚原因。

更新1

我添加了三个测试用例:testNotMatch_03()testNotMatch_04()testNotMatch_05()。他们对给定的正则表达式运行正常。但testMatch_02()testNotMatch_01()仍然如前所述失败。我已经决定添加这些新的测试用例,以澄清在 .NET 之前不会总是有空格。

更新2

我已将一些模式从(?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)更改为(?i)(.*)(?!http\\:\\/\\/)(.*)(\\.net)(?!work|flix)(.*)。因此,除testNotMatch_01()外,所有测试用例都运行正常。我已经更新了测试代码,以防有人想用这种新模式运行它。

更新3

拜托,我将非常感谢有人之前运行测试用例并根据其进行假设。我们可以避免在聊天对话中提出这个问题。

更新4

重要的是要说不仅列出的例子必须通过,而且正则表达式必须根据问题的原始措辞中描述的内容进行验证。在与@Thomas交谈之后,我已经包含了三个新匹配和两个新的不匹配示例,因此代码中的每个示例的测试用例以及@Thomas提供的正则表达式。另外,我已经将代码更改为@Thomas提供的代码,它更简单,更短,作为他的正则表达式。

package com.regex;

public class TestRegex
{
    //private static final String regex = "(?i)(.*)(?!.*http\\:\\/\\/.*)(\\.net)(?!.*work)(?!.*flix)(.*)";
    //private static final String regex = "(?i)(.*)(?!http\\:\\/\\/)(.*)(\\.net)(?!work|flix)(.*)";
    private static final String regex = "(?i).*( |microsoft).net($|Framework)"; //@Thomas

    public static void main(String[] args)
    {
        String str = "The framework .NET";
        System.out.println("testMatch_01() must match: [" + str + "] =>  " + str.matches(regex));

        str = "That is .NETFramework";
        System.out.println("testMatch_02() must match: [" + str + "] =>  " + str.matches(regex));

        str = "Microsoft.NET";
        System.out.println("testMatch_03() must match: [" + str + "] =>  " + str.matches(regex));

        str = "That is .netframework";
        System.out.println("testMatch_04() must match: [" + str + "] =>  " + str.matches(regex));

        str = ".netframework";
        System.out.println("testMatch_05() must match: [" + str + "] =>  " + str.matches(regex));

        str = ".NETFramework";
        System.out.println("testMatch_06() must match: [" + str + "] =>  " + str.matches(regex));

        str = "This is a URL http://www.my.net";
        System.out.println("testNotMatch_01() must not match: [" + str + "] =>  " + str.matches(regex));

        str = "The Network isn't matching because the missing point";
        System.out.println("testNotMatch_02() must not match: [" + str + "] =>  " + str.matches(regex));

        str = "The .NETwork is up";
        System.out.println("testNotMatch_03() must not match: [" + str + "] =>  " + str.matches(regex));

        str = "Microsoft.NetworkAndSharingCenter";
        System.out.println("testNotMatch_04() must not match: [" + str + "] =>  " + str.matches(regex));

        str = "4df9e0f8.netflix_mcm4njqhnhss8";
        System.out.println("testNotMatch_05() must not match: [" + str + "] =>  " + str.matches(regex));
    }

}

以上代码的输出为:

正则表达式(?i)(.*)(?!http\\:\\/\\/)(.*)(\\.net)(?!work|flix)(.*)
testNotMatch_01()失败

testMatch_01() must match: [The framework .NET] =>  true
testMatch_02() must match: [That is .NETFramework] =>  true
testMatch_03() must match: [Microsoft.NET] =>  true
testMatch_04() must match: [That is .netframework] =>  true
testMatch_05() must match: [.netframework] =>  true
testMatch_06() must match: [.NETFramework] =>  true
testNotMatch_01() must not match: [This is a URL http://www.my.net] =>  true
testNotMatch_02() must not match: [The Network isn't matching because the missing point] =>  false
testNotMatch_03() must not match: [The .NETwork is up] =>  false
testNotMatch_04() must not match: [Microsoft.NetworkAndSharingCenter] =>  false
testNotMatch_05() must not match: [4df9e0f8.netflix_mcm4njqhnhss8] =>  false

正则表达式(?i).*( |microsoft).net($|Framework)
testMatch_05()testMatch_06()失败

testMatch_01() must match: [The framework .NET] =>  true
testMatch_02() must match: [That is .NETFramework] =>  true
testMatch_03() must match: [Microsoft.NET] =>  true
testMatch_04() must match: [That is .netframework] =>  true
testMatch_05() must match: [.netframework] =>  false
testMatch_06() must match: [.NETFramework] =>  false
testNotMatch_01() must not match: [This is a URL http://www.my.net] =>  false
testNotMatch_02() must not match: [The Network isn't matching because the missing point] =>  false
testNotMatch_03() must not match: [The .NETwork is up] =>  false
testNotMatch_04() must not match: [Microsoft.NetworkAndSharingCenter] =>  false
testNotMatch_05() must not match: [4df9e0f8.netflix_mcm4njqhnhss8] =>  false

1 个答案:

答案 0 :(得分:0)

这个正则表达式适用于所有示例:

center {
  text-align: left;
}

请参阅live demo

请注意,您的反面示例&#34;网络不匹配,因为缺少点&#34;是误导,因为它也不应该匹配因为&#34; .net&#34;之后是&#34; work&#34;。