Search code examples
c#regexviewmodelarabic

regex for Arabic and English letter(in viewmodel-c#)


I would like to have a regular expression that matches:

1.Arabic letters.

2.English letters.

3.Allow space.

4.min 2-max 30.

then i wrotre this regex:

^(?:[a-zA-Z\s\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDCF\uFDF0-\uFDFF\uFE70-\uFEFF]|(?:\uD802[\uDE60-\uDE9F]|\uD83B[\uDE00-\uDEFF])[ ]{0,1}){2,30}$

but its not good


Solution

  • If an Arabic letter regex is [\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF] (see Regular Expression not to allow numbers - just Arabic letters) and English letters are [a-zA-Z], you can use

    ^(?=.{2,30}$)[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z]+(?:\s[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z]+)?$
    

    Details:

    • ^ - start of string
    • (?=.{2,30}$) - the string must be 2 to 30 chars long
    • [\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z]+ - one or more Arabic or English letters
    • (?:\s[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FFa-zA-Z]+)? - an optional occurrence of a whitespace and one or more Arabic or English letters
    • $ - end of string.