我指的是此处列出的测试工具http://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html
我对类所做的唯一更改是模式创建如下:
Pattern pattern =
Pattern.compile(console.readLine("%nEnter your regex(Pattern.CANON_EQ set): "),Pattern.CANON_EQ);
作为 http://docs.oracle.com/javase/tutorial/essential/regex/pattern.html 的教程建议我将模式或正则表达式作为 a\u030A
和要匹配的字符串作为 \u00E5
输入,但它以未找到匹配项结束。我看到两个字符串都是一个顶部带有圆环的小盒子“a”。
我没有正确理解用例吗?
最佳答案
您看到的行为与 Pattern.CANON_EQ
标志无关。
从控制台读取的输入与 Java 字符串文字不同。当用户(假设是您,正在测试此标志)在控制台中键入 \u00E5
时,console.readLine
读取的结果字符串等同于 "\\u00E5”
,而不是“å”。自己看看:http://ideone.com/lF7D1
至于Pattern.CANON_EQ
,它的行为与描述的完全一样:
Pattern withCE = Pattern.compile("^a\u030A$",Pattern.CANON_EQ);
Pattern withoutCE = Pattern.compile("^a\u030A$");
String input = "\u00E5";
System.out.println("Matches with canon eq: "
+ withCE.matcher(input).matches()); // true
System.out.println("Matches without canon eq: "
+ withoutCE.matcher(input).matches()); // false
关于java - 模式中的规范等价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10265380/