17

I found this regular expression for Arabic letters but it is also allowing numbers with letters. How can I change it to let it allow letters only ?

/[\u0600-\u06FF]/
Noon
  • 1,181
  • 6
  • 20
  • 38

5 Answers5

30

Probably you'd have to check what range the numbers match and exclude it (formally not include in brackets expression).

Here I've found another helpful source.

I'd suggest this for only letters

/[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]/

as this matches arabic digits only

/[\u0660-\u0669\u06F0-\u06F9]/

Edit:

I've found that there are two ranges for arabic and arabic-indic digits in unicode.

If you need a regex to match a line just then, when it contains arabic letters and numbers - use this:

/^[\u0600-\u06FF]*$/

If you want to also discourage arabic digits - use this:

/^[\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]*$/

If you want to match a substring, not only a whole line, use this:

/\b[\s\u0600-\u065F\u066A-\u06EF\u06FA-\u06FF]*\b/
Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
  • Thanks but this will include numbers 0-9 too – Noon Sep 20 '12 at 18:58
  • I'm pretty sure there are no westernized arabic digits as we use them 0-9 in the range `u0600-u06ff`. Check yourself [here](http://www.alanwood.net/unicode/arabic.html). Maybe what you want, is a regex, that disallows a line where a number is contained. Gimme more context, so I could help you. – Krzysztof Jabłoński Sep 20 '12 at 19:06
  • Thanks a lot! yes you are right I don't want any numbers even english numbers. I just want regex to allow arabic string. – Noon Sep 20 '12 at 19:20
  • Thank you so much!! I really appreciate your detailed answer. – Noon Sep 20 '12 at 19:47
  • All of the mentioned RegExes select English/Latin Letter too. How to exclude Latin characters? – Junaid Qadir Shekhanzai Mar 17 '21 at 20:03
7

I tried all solutions provided here, nothing worked, finally one solution worked for me for Arabic letters only

^[\u0621-\u064A\040]+$
josliber
  • 43,891
  • 12
  • 98
  • 133
amal50
  • 981
  • 2
  • 21
  • 35
2
[RegularExpression(@"^[\u0621-\u064A\u0660-\u0669a-zA-Z]+$", ErrorMessage = "You can enter Arabic or English characters only")] 

[RegularExpression(@"^[0-9]+$", ErrorMessage = "You can enter numbers only")]

[RegularExpression(@"^[a-zA-Z\0-9]+$",ErrorMessage = "You can enter numbers or english characters only")]

[RegularExpression(@"^[\u0621-\u064A\u0660-\u0669\0-9]+$", ErrorMessage = "You can enter numbers or arabic characters only")]

[RegularExpression(@"^[\u0621-\u064A\u0660-\u0669]+$", ErrorMessage = "You can enter arabic characters only")]

[RegularExpression(@"^[a-zA-Z]+$",ErrorMessage = "You can enter english characters only")]
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

First, concerning Arabic encoding in unicode, you might want to refer to this tables here

As for the regex you were given, [\u0600-\u06FF] is the range of all arabic characters on the unicode listing, definitely that includes even letters, control characters, white-space, and numbers.

My recommendation would be:

/[\u0600-\u06FF&&[^\U06F0-\06F9]]/

Which spans just everything minus the arabic numeric digits (0-9).

That subtracts a range from the 'super' range. Am just not sure whether your target regex dialect supports this.

JWL
  • 13,591
  • 7
  • 57
  • 63
0

I'd suggest this for only letters, words and sentences [\sء-ي]+