我正在尝试匹配一些可能包含 Unicode 字符的文本,其中包括特殊标点符号,例如 (Java 中的 \u0085
)。
当我做类似的事情时
Matcher testMatcher = Pattern.compile("(.+)", Pattern.UNICODE_CHARACTER_CLASS).matcher("test text up \u0085 after");
我得到了“test text up”的匹配项,没有标点符号,但我想匹配所有内容。我该怎么做?
另见 demonstration in the regex101 tool .
更新:我试过 ((?:\P{M}\p{M}*+)+)
as discussed at regular-expressions.info , 但它似乎不适用于 Java。
最佳答案
符号属于Cc - Other, control 类别。
您需要添加Pattern.DOTALL
修饰符来匹配它。或者在模式开始处附加 (?s)
。
General category: Cc - Other, control
Canonical combining class: 0 - Spacing, split, enclosing, reordrant, & Tibetan subjoined
Bidirectional category: B - Paragraph separator
Unicode 1.0 name: NEXT LINE (NEL)
Unicode version: 1.1
As text:
Decimal: 133
HTML escape: …
URL escape: %C2%85
参见 details here
这是一个 IDEONE demo
Matcher testMatcher = Pattern.compile(".+", Pattern.DOTALL | Pattern.UNICODE_CHARACTER_CLASS).matcher("test text up \u0085 after");
if (testMatcher.find()){
System.out.println(testMatcher.group(0));
} // => test text up after
关于java - 匹配所有内容包括unicode标点符号的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35148343/