我有正则表达式来验证数字和 - 。我现在也支持mutibyte字符。所以我使用了unicode类来支持但它不匹配。有人可以启发我吗
public class Test123 {
public static void main(String[] args) {
String test="熏肉еконcarácterbañlácaractères" ;
Pattern pattern = Pattern.compile("^[a-zA-Z0-9_-]*$",Pattern.UNICODE_CHARACTER_CLASS);
Matcher matcher = pattern.matcher(test);
if(matcher.matches())
{
System.out.println("matched");
}
else{
System.out.println("not matched");
}
}
}
答案 0 :(得分:4)
您可以使用posix类\\p{Alpha}
,而不是使用[a-zA-Z]
的文字类来匹配unicode和重音字符。
示例强>
String test = "熏肉еконcarácterbañlácaractères";
Pattern pattern = Pattern.compile("\\p{Alpha}+", Pattern.UNICODE_CHARACTER_CLASS);
Matcher m = pattern.matcher(test);
while (m.find()) {
System.out.println(m.group());
}
<强>输出强>
熏肉еконcarácterbañlácaractères
答案 1 :(得分:1)
问题在于,尽管该标志a-z
并不表示&#34; 所有Unicode字母字符&#34;但只有a
和z
&#34;之间的&#34; 字符。
UNICODE_CHARACTER_CLASS
标志仅将Unicode支持添加到预定义字符类,例如\w
,通常代表a-zA-Z0-9_
。
请尝试使用
Pattern.compile("^[\\w-]*$",Pattern.UNICODE_CHARACTER_CLASS);
答案 2 :(得分:0)
[\\p{L}\\p{M}]+
您可以使用它来匹配unicode
个字母。
\p{L} matches any kind of letter from any language
\p{M} matches a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)
参见演示。