Java 正则表达式在任何位置有一个匹配和两个否定
Java Regex with one matching and two negations in any position
我必须匹配关键字,前提是它不在复合 URL 或某些单词的句子中。比如关键字.NET,字符串不能有http://,.NET后面的字符 不能是 work 或 flix,但可以是 framework 或任何其他词,甚至什么都没有。正则表达式必须不区分大小写。
我有这些例子可以匹配:
- 框架.NET
- 也就是.NETFramework
- 微软.NET
- .NET框架(更新)
- .net框架(更新)
- .net(更新)
这些例子不匹配:
- 这是一个URLhttp://www.my.net/不匹配
- 网络不匹配,因为缺少点
- .NET工作完成
- Microsoft.NetworkAndSharingCenter
- 4df9e0f8.netflix_mcm4njqhnhss8
- .net工作(更新)
- .Net工作(更新)
我写过这个模式:
(?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)
我已经在下面编写了这些测试用例的代码,但是 testMatch_02()
和 testNotMatch_01()
都失败了,我无法弄清楚原因。
更新 1
我又添加了三个测试用例:testNotMatch_03()
、testNotMatch_04()
和 testNotMatch_05()
。他们 运行ning 可以针对给定的正则表达式。但是 testMatch_02()
和 testNotMatch_01()
仍然如前所述失败。我决定添加这些新的测试用例,以澄清在 .NET.
之前并不总是会有 space
更新 2
我已经将一些模式从 (?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)
更改为 (?i)(.*)(?!http\:\/\/)(.*)(\.net)(?!work|flix)(.*)
。因此所有测试用例 运行 OK 除了 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
此正则表达式适用于您的所有示例:
^(?i)(?!.*http:\/\/).*?\.net(?!work|flix).*
参见live demo。
请注意,您的负面示例 "The Network isn't matching because the missing point" 具有误导性,因为它也不应该匹配,因为“.net”后跟 "work"。
我必须匹配关键字,前提是它不在复合 URL 或某些单词的句子中。比如关键字.NET,字符串不能有http://,.NET后面的字符 不能是 work 或 flix,但可以是 framework 或任何其他词,甚至什么都没有。正则表达式必须不区分大小写。
我有这些例子可以匹配:
- 框架.NET
- 也就是.NETFramework
- 微软.NET
- .NET框架(更新)
- .net框架(更新)
- .net(更新)
这些例子不匹配:
- 这是一个URLhttp://www.my.net/不匹配
- 网络不匹配,因为缺少点
- .NET工作完成
- Microsoft.NetworkAndSharingCenter
- 4df9e0f8.netflix_mcm4njqhnhss8
- .net工作(更新)
- .Net工作(更新)
我写过这个模式:
(?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)
我已经在下面编写了这些测试用例的代码,但是 testMatch_02()
和 testNotMatch_01()
都失败了,我无法弄清楚原因。
更新 1
我又添加了三个测试用例:testNotMatch_03()
、testNotMatch_04()
和 testNotMatch_05()
。他们 运行ning 可以针对给定的正则表达式。但是 testMatch_02()
和 testNotMatch_01()
仍然如前所述失败。我决定添加这些新的测试用例,以澄清在 .NET.
更新 2
我已经将一些模式从 (?i)(.*)(?!.*http\:\/\/.*)(\.net)(?!.*work)(?!.*flix)(.*)
更改为 (?i)(.*)(?!http\:\/\/)(.*)(\.net)(?!work|flix)(.*)
。因此所有测试用例 运行 OK 除了 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
此正则表达式适用于您的所有示例:
^(?i)(?!.*http:\/\/).*?\.net(?!work|flix).*
参见live demo。
请注意,您的负面示例 "The Network isn't matching because the missing point" 具有误导性,因为它也不应该匹配,因为“.net”后跟 "work"。