如何知道文本是阿拉伯语还是乌尔都语
How to know text is Arabic or in Urdu
我想知道文本是否包含乌尔都语或阿拉伯语中的任何字母。使用这种条件会在特殊字符 comes.what 是正确的方法时产生错误结果。任何库或什么是正确的正则表达式这个?
if (cap.replaceAll("\s+", "").matches("[A-Za-z]+")
|| cap.replaceAll("\s+", "").matches("[A-Za-z0-9]+")) {
Log.d("isUrdu", "false");
caption.setTypeface(Typeface.DEFAULT);
caption.setTextSize(16);
} else {
Log.d("isUrdu", "True");
/* if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {*/
caption.setTypeface(typeface);
caption.setTextSize(20);
/* }*/
}
在这里试试这个你可以知道文本是否是阿拉伯语
var arregex = /[\u0600-\u06FF]/;
var test = arregex.test(text);
return test;
这里可以不用正则表达式,你只需要找到阿拉伯语和乌尔都语 UTF 字符范围,然后检查输入的文本是否匹配该范围。
看看 Wikipedia Urdu alphabet,它包括以下 Unicode 范围:
U+0600 to U+06FF
U+0750 to U+077F
U+FB50 to U+FDFF
U+FE70 to U+FEFF
要匹配阿拉伯字母,您可以使用 \p{InArabic}
Unicode 属性 class.
所以,您可以使用
if (cap.matches("(?s).*[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF].*"))
{
/*There is an Urdu character*/
}
else if (cap.matches("(?s).*\p{InArabic}.*"))
{
/* The string contains an Arabic character */
}
else { /*No Arabic nor Urdu chars detected */ }
请注意 (?s)
启用 DOTALL
修饰符以便 .
也可以匹配换行符。
为了使用 matches
获得更好的性能,您可以使用反向 classes 而不是第一个 .*
:分别是 "(?s)[^\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]*[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF].*"
和 "(?s)\P{InArabic}*\p{InArabic}.*"
。
请注意,您还可以使用更短的 "[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]"
和 "\p{InArabic}"
模式以及 Matcher#find()
。
我想知道文本是否包含乌尔都语或阿拉伯语中的任何字母。使用这种条件会在特殊字符 comes.what 是正确的方法时产生错误结果。任何库或什么是正确的正则表达式这个?
if (cap.replaceAll("\s+", "").matches("[A-Za-z]+")
|| cap.replaceAll("\s+", "").matches("[A-Za-z0-9]+")) {
Log.d("isUrdu", "false");
caption.setTypeface(Typeface.DEFAULT);
caption.setTextSize(16);
} else {
Log.d("isUrdu", "True");
/* if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {*/
caption.setTypeface(typeface);
caption.setTextSize(20);
/* }*/
}
在这里试试这个你可以知道文本是否是阿拉伯语
var arregex = /[\u0600-\u06FF]/;
var test = arregex.test(text);
return test;
这里可以不用正则表达式,你只需要找到阿拉伯语和乌尔都语 UTF 字符范围,然后检查输入的文本是否匹配该范围。
看看 Wikipedia Urdu alphabet,它包括以下 Unicode 范围:
U+0600 to U+06FF
U+0750 to U+077F
U+FB50 to U+FDFF
U+FE70 to U+FEFF
要匹配阿拉伯字母,您可以使用 \p{InArabic}
Unicode 属性 class.
所以,您可以使用
if (cap.matches("(?s).*[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF].*"))
{
/*There is an Urdu character*/
}
else if (cap.matches("(?s).*\p{InArabic}.*"))
{
/* The string contains an Arabic character */
}
else { /*No Arabic nor Urdu chars detected */ }
请注意 (?s)
启用 DOTALL
修饰符以便 .
也可以匹配换行符。
为了使用 matches
获得更好的性能,您可以使用反向 classes 而不是第一个 .*
:分别是 "(?s)[^\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]*[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF].*"
和 "(?s)\P{InArabic}*\p{InArabic}.*"
。
请注意,您还可以使用更短的 "[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]"
和 "\p{InArabic}"
模式以及 Matcher#find()
。