How to check persian character format in regex

2.2k views Asked by At

How can to use regex for all utf8 character? for example I want to check this format by regex:

[1][الهه اردونی]

I used \w for checking persian character but it dosent worked:

^(\[1\])(\[\w+\])$

I also used this:

^(\[1\])(\[\u0600-\u06FF\])$

so how can I do that? Thanks for any helping

3

There are 3 answers

0
nu11p01n73R On BEST ANSWER

How about the regex

^(\[1\])\[[\p{L}\s]+\]$

example : http://regex101.com/r/cU1nQ8/1

  • \p{L} matches any kind of letter from any language
0
Avinash Raj On

You're almost there. You just need to include the range \u0600-\u06FF, the pattern to match spaces \s inside a character class like below.

^(\[1\])(\[[\u0600-\u06FF\s]+\])$

DEMO

String input = @"[1][الهه اردونی]";
Regex rgx = new Regex(@"^(\[1\])(\[[\u0600-\u06FF\s]+\])$");
foreach (Match m in rgx.Matches(input))
{
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(m.Groups[2].Value);
}

Output:

[1]
[الهه اردونی]

IDEONE

[\u0600-\u06FF\s]+ match one or more characters from the given list. - acts like a range operator only inside the character class.

0
Farahmand On

You can use something like so:

^(\[1\])(\[[ا-ی\s]+\])$