仅接受波斯字符的正则表达式 [英] regex for accepting only persian characters
问题描述
我正在开发一种表单,其中一个自定义验证器应该只接受波斯语字符.我使用了以下代码:
var myregex = new Regex(@"^[u0600-u06FF]+$");如果(myregex.IsMatch(mytextBox.Text)){args.IsValid = true;}别的{args.IsValid = false;}
但是,它似乎只能检测阿拉伯字符,因为它没有涵盖所有波斯字符(它缺少这四个: گ,چ,پ,ژ ).
有没有办法解决这个问题?
TL;DR
波斯语必须使用的字符集如下:
使用
整个故事
这个答案的存在是为了解决一个常见的误解.代码点
0600
到06FF
不表示 波斯语/波斯语字母([آ-ی]
):[u0600-u0605 ؐ-ؚu061Cـ ۖ-u06DD ۟-ۤ ۧ ۨ ۪-ۭ ً-ٕ ٟ ٖ-ٞ ٰ ، ؍ ٫ ٬ ؛柞ㄔ٭ ٪ ؉ ؊ ؈ ؎ ؏۞ ۩ ؆ ؇ ؋ ٠۰ ١۱ ٢۲ ٣۳ ٤۴ ٥۵ ٦۶ ٧۷ ٨۸ ٩۹ ءٴ۽ آ أ ٲ ٱ ؤ إٳ ك پ پة-ث ٹ ٺ ټ ٿ ج ڃ ڄ چ ڿ ه ح خ ڂ څ د ذ ڈ-ڐ ۮ ر ز ڑ-ڶ ۯ س ش ښ- ڝ ۶ۻ ط ظ ع غ ڠ ۼ ف ڡ-ڦ ٯ ق ڧ ڨ ك ک-ڴ ػ ؼ ل ڵ-ڸ م۾ ن ں-ڽ ڹ ه ۾ ہەوە ۻ从
255 个字符属于阿拉伯语块 (0600–06FF)、波斯语字母表有 32 个字母,除了波斯语演示数字之外,它将是 42 个.如果我们添加元音(阿拉伯元音最初,在波斯语中很少使用)没有 Tanvin (
ً
,ٍِ
、ٌ
) 和 Tashdid (ّ
) 都是阿拉伯语变音符号的子集,不是波斯语,我们最终会得到 46 个字符.这意味着u0600-u06FF
包含的字符比您需要的多 209 个!
<块引用>۷
with codepoint06F7
是数字7
和٧
的波斯语表示,codepoint0667
是相同数字的阿拉伯语表示.۶
是数字6
的波斯语表示,٦
是相同数字的阿拉伯语表示.并且都驻留在0600
到06FF
代码点中.波斯数字四 (
۴
)、五 (۵
) 和六 (۶
) 的形状是与阿拉伯语中使用的形状不同,其他数字有不同的代码点.您也可以看到波斯语/波斯语中不存在的不同数量的其他字符,并且没有人愿意在验证名字或姓氏时使用它们.
[آ-ی]
也包含 117 个字符,这远远超出了验证所需的字符数.您可以使用 Unicode CLDR 查看所有这些内容.I'm working on a form where one of its custom validators should only accept Persian characters. I used the following code:
var myregex = new Regex(@"^[u0600-u06FF]+$"); if (myregex.IsMatch(mytextBox.Text)) { args.IsValid = true; } else { args.IsValid = false; }
However, it seems that it can only detect Arabic characters, as it doesn't cover all Persian characters (it lacks these four: گ,چ,پ,ژ ).
Is there a way to solve this problem?
解决方案TL;DR
Farsi MUST used character sets are as following:
Use
^[آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی]+$
for letters or use codepoints regarding your regex flavor (not all engines supportuXXXX
notation):^[u0622u0627u0628u067Eu062A-u062Cu0686u062D-u0632u0698u0633-u063Au0641u0642u06A9u06AFu0644-u0648u06CC]+$
Use
^[۰۱۲۳۴۵۶۷۸۹]+$
for numbers or regarding your regex flavor:^[u06F0-u06F9]+$
Use
[ ٌ ًّ َ ِ ُ ْ ]
for vowels or regarding your regex flavor:[u202Cu064Bu064Cu064E-u0652]
or a combination of those together. You may want to add other Arabic letters like Hamza
ء
to your character set additionally.Why are
[u0600-u06FF]
and[آ-ی]
both wrong?Although
u0600-u06FF
includes:گ
with codepoint06AF
چ
with codepoint0686
پ
with codepoint067E
ژ
with codepoint0698
as well, all answers that suggest
[u0600-u06FF]
or[آ-ی]
are simply WRONG.i.e.
u0600-u06FF
contains 209 more characters than you need! and it includes numbers too!Whole story
This answer exists to fix a common misconception. Codepoints
0600
through06FF
do not denote Persian / Farsi alphabet (neither does[آ-ی]
):[u0600-u0605 ؐ-ؚu061Cـ ۖ-u06DD ۟-ۤ ۧ ۨ ۪-ۭ ً-ٕ ٟ ٖ-ٞ ٰ ، ؍ ٫ ٬ ؛ ؞ ؟ ۔ ٭ ٪ ؉ ؊ ؈ ؎ ؏ ۞ ۩ ؆ ؇ ؋ ٠۰ ١۱ ٢۲ ٣۳ ٤۴ ٥۵ ٦۶ ٧۷ ٨۸ ٩۹ ءٴ۽ آ أ ٲ ٱ ؤ إ ٳ ئ ا ٵ ٮ ب ٻ پ ڀ ة-ث ٹ ٺ ټ ٽ ٿ ج ڃ ڄ چ ڿ ڇ ح خ ځ ڂ څ د ذ ڈ-ڐ ۮ ر ز ڑ-ڙ ۯ س ش ښ-ڜ ۺ ص ض ڝ ڞ ۻ ط ظ ڟ ع غ ڠ ۼ ف ڡ-ڦ ٯ ق ڧ ڨ ك ک-ڴ ػ ؼ ل ڵ-ڸ م۾ ن ں-ڽ ڹ ه ھ ہ-ۃ ۿ ەۀ وۥ ٶ ۄ-ۇ ٷ ۈ-ۋ ۏ ى يۦ ٸ ی-ێ ې ۑ ؽ-ؿ ؠ ے ۓ u061D]
255 characters are fallen under Arabic block (0600–06FF), Farsi alphabet has 32 letters that in addition to Farsi demonstration of digits it would be 42. If we add vowels (Arabic vowels originally, that rarely used in Farsi) without Tanvin (
ً
,ٍِ
,ٌ
) and Tashdid (ّ
) that are both a subset of Arabic diacritics not Farsi, we would end up with 46 characters. This meansu0600-u06FF
contains 209 more characters than you need!۷
with codepoint06F7
is a Farsi representation of number7
and٧
with codepoint0667
is Arabic representation of the same number.۶
is Farsi representation of number6
and٦
is Arabic representation of the same number. And all reside in0600
through06FF
codepoints.The shapes of the Persian digits four (
۴
), five (۵
), and six (۶
) are different from the shapes used in Arabic and the other numbers have different codepoints.You can see different number of other characters that doesn't exist in Farsi / Persian too and nobody is willing to have them while validating a first name or surname.
[آ-ی]
includes 117 characters too which is much more than what someone needs for validation. You can see them all using Unicode CLDR.这篇关于仅接受波斯字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!