我想定位/替换文本中的单词。单词示例是“TÜTÜ”。这是代码:
final String regexX = "TÜTÜ";
final String string = "dsad dasdasd dasd \n"
+ "dsds\n"
+ " dd \n"
+ "sadsd.sdasd. \n"
+ " universität \n"
+ " blö \n"
+ " Blö\n"
+ " ble\n"
+ "üeee \n"
+ " Wörterbuch \n"
+ "Das gute alte Tütü wird";
final String subst = "";
final Pattern pattern = Pattern.compile(regexX, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcherX = pattern.matcher(string);
final String result = matcherX.replaceAll(subst);
System.out.println("Substitution result: " + result);
结果是,没有任何内容被替换。这实际上是从 regex101.com 复制的代码,其中德语 TÜTÜ 也无法识别。不区分大小写对德语“Umlaute”不起作用是真的吗?还是有办法让它起作用?
最佳答案
final Pattern pattern = Pattern.compile(regexX, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
您需要添加Pattern.UNICODE_CASE ,否则仅 US-ASCII 字符集用于 CASE_INSENSITIVE:
Enables Unicode-aware case folding. When this flag is specified then case-insensitive matching, when enabled by the CASE_INSENSITIVE flag, is done in a manner consistent with the Unicode Standard. By default, case-insensitive matching assumes that only characters in the US-ASCII charset are being matched.
Unicode-aware case folding can also be enabled via the embedded flag expression (?u).
Specifying this flag may impose a performance penalty.
关于Java 正则表达式 不区分大小写,带有德语变音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58970791/