regex for accepting only persian characters(只接受波斯字符的正则表达式)
问题描述
我正在开发一种表单,其中一个自定义验证器应该只接受波斯字符.我使用了以下代码:
var myregex = new Regex(@"^[u0600-u06FF]+$");if (myregex.IsMatch(mytextBox.Text)){args.IsValid = true;}别的{args.IsValid = 假;}但是,它似乎只能检测阿拉伯字符,因为它没有涵盖所有波斯字符(它缺少这四个:گ,چ,پ,ژ).
有没有办法解决这个问题?
TL;DR
波斯语必须使用的字符集如下:
使用
整个故事
这个答案的存在是为了纠正一个常见的误解.代码点
0600到06FF不表示 波斯语/波斯语字母([آ-ی]也不行):[u0600-u0605 ؐ-ؚu061Cـ ۖ-u06DD ۟-8 ۧ ۨ ۪-ۭ ً-ٕ ٟ ٖ-ٞ ٰ ، ؍ ٫ ٬ ؛؞ ؟3٭ ٪ ؉ ؊ ؈ ؎ ؏۞۩ ؆ ؇ ؋ ٠۰ ١۱ ٢۲ ٣۳ ٤۴ ٥۵ ٦۶ ٧۷ ٨۸ ٩۹ ءٴ۽ آ أ ٲ ٱ ؤ إ ٳ ئ ا ٵ ٮ ب ٻ پ ڀة-ثٹٺټٽٿۻ ط ظ ڟ ع غ ۼ ف ڡ-ڦ ٯ ق ڧ ك ک-ڴ ػ ؼ ل ڵ-ڸ م۾ ن ں-ڽ ه ھ ہ-ۃ ۿ ەۀ وۥ ٶۄ-8 ٷ ۈ-ۋ ۏ ى يۦ ٸ ی-ێ ې ۑ ؽ-ؿ ؠ ے ۓ u061D]255 个字符属于 阿拉伯语块 (0600–06FF),波斯语字母有 32 个字母,除了波斯语的数字演示之外,它将是 42.如果我们添加元音(最初是阿拉伯语元音,在波斯语中很少使用)而不使用 Tanvin(
ً,ٍِ、ٌ) 和 Tashdid (ّ) 都是阿拉伯语变音符号而非波斯语的子集,我们最终会得到 46 个字符.这意味着u0600-u06FF包含的字符比您需要的多 209 个!
<块引用>7代码点06F7是数字7和٧代码点0667 的波斯语表示是相同数字的阿拉伯语表示.۶是数字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 supportuXXXXnotation):^[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-u06FFincludes:گwith codepoint06AFچwith codepoint0686پwith codepoint067Eژwith codepoint0698
as well, all answers that suggest
[u0600-u06FF]or[آ-ی]are simply WRONG.i.e.
u0600-u06FFcontains 209 more characters than you need! and it includes numbers too!Whole story
This answer exists to fix a common misconception. Codepoints
0600through06FFdo 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-u06FFcontains 209 more characters than you need!۷with codepoint06F7is a Farsi representation of number7and٧with codepoint0667is Arabic representation of the same number.۶is Farsi representation of number6and٦is Arabic representation of the same number. And all reside in0600through06FFcodepoints.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.这篇关于只接受波斯字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:只接受波斯字符的正则表达式
基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
