我想在 Java 中动态生成正则表达式模式:
String s = "\uD83E\uDD81";
int codePoint = Character.toCodePoint(s.charAt(0), s.charAt(1));
String pattern = "\\x{" + String.valueOf(Integer.toHexString(codePoint)).toUpperCase() + "}";
Matcher matcher = Pattern.compile(pattern).matcher(s);
System.out.println(matcher.matches());
此代码可以正确编译并运行。但是,IntelliJ IDEA 对于 "\\x{"
给出以下错误 '非法十六进制转义序列
。
我正在使用 Java 8 和 IntelliJ IDEA 2018.1
这是一个错误吗?问题似乎出在 IntellJ 对传递给 Pattern.compile
方法的参数进行的分析中。
最佳答案
您发布的代码编译并运行没有问题。如果您使用相同的文字字符串创建模式,IntelliJ 不会提示:
String s = "\uD83E\uDD81";
String pattern = "\\x{1F981}";
Matcher matcher = Pattern.compile(pattern).matcher(s);
System.out.println(matcher.matches());
所以,是的,这看起来像是 IntelliJ 中的一个错误。
关于java - IntelliJ 在我的有效字符串上报告 'illegal hexadecimal escape sequence',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52433284/