对于我希望匹配变音符号的名称,我有一个正则表达式。这是我的代码中以 test.java:191 开头的日志记录片段:
Util.Log("text = " + text);
Util.Log("regex = " + regex);
Util.Log("regexorig = " + regexorig);
Util.Log("Matches static: " + Pattern.matches(text, regex));
Pattern p1 = Pattern.compile(regex);
Util.Log("Matches p1: " + p1.matcher(text).matches());
Pattern p2 = Pattern.compile(regexorig, Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
Util.Log("Matches p2: " + p2.matcher(text).matches());
Util.Log("String matches: " + text.matches(regex));
这是我使用输入“ü”时的输出:
LOG: (test.java:191):text = ü
LOG: (test.java:192):regex = (?iu)[A-Z][A-Z0-9 \-.',]*
LOG: (test.java:193):regexorig = [A-Z][A-Z0-9 \-.',]*
LOG: (test.java:194):Matches static: false
LOG: (test.java:196):Matches p1: false
LOG: (test.java:198):Matches p2: false
LOG: (test.java:199):String matches: false
我似乎无法使变音符号不敏感的正则表达式匹配工作。这是Android错误还是我错过了什么?根据文档,对于不区分大小写的 Android 字符串,UNICODE_CASE 始终处于启用状态,所以我什至不需要它(真的不知道为什么会这样,但这是另一个讨论的问题)。