我想在 Java 中为不包含“R2”的 Windows Server 2008 操作系统版本找到一个正则表达式 我目前使用的正则表达式 -
(?i)Win\w*\s*(?i)Server\s*(2008)\s*(?!R2)\s*\w*
可能的值:
Windows Server 2008 数据中心
- 正确匹配Windows Server 2008
- 正确匹配Windows Server 2008 R2 Datacenter
- 不匹配Windows Server 2008 r2 数据中心
- 不匹配Windows Server 2008 R2
- 匹配不正确(因为R2
适合正则表达式中的\w*
)
我在正则表达式中做错了什么?
最佳答案
您可以考虑使用以下正则表达式:
(?i)Win\w*\s*Server\s*(2008)(?!\sR2).*?$
参见 regex demo
Java ( demo )
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class RegEx {
public static void main(String[] args) {
String s = "Windows Server 2008 datacenter";
String r = "(?i)Win\\w*\\s*Server\\s*(2008)(?!\\sR2).*?$";
Pattern p = Pattern.compile(r);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
}
关于Java- 不包含后跟任何字符的子字符串的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721559/