Skip to content

Regex for Arabic#

Dealing with Arabic characters in Regular Expressions is a different story. In this website, I try to provide developers and computational linguists with tools and methods on how to apply NLP tasks on Arabic.

Character Classes#

- Character Sets#

The character set [ ] matches any character in the set, i.e., any character between the two brackets.

Beware that Arabic characters do not have space in between. They are - almost - connected. We only add space between words. For example, If you would like to put a character set that matches أ or ب or ت. It will look like [أبت]

[أبت]

مرحـبـا أخي تـميم

[abc]

Good morning brother Tamim


- Negated Sets#

The negated set [^ ] matches any character that is not in the set, i.e., any character outside the two brackets. For example, If we would like to put a character set that matches everything but أ or ب or ت. It will look like [^أبت]

[^أبت]

مرحـبــا أخي تـميم

[^abc]

Good morning brother Tamim


- Range#

As mentioned in regexr website The Range "Matches a character having a character code between the two specified characters inclusive."

In English, the whole alphabet range is is from a to z. And if you want only a subset of this range, you would write for example r-z. Which means that you want to match all the characters between r and z (Lowercase).

In Arabic, the alphabet starts with Alif (أ) but it is used in regex as Hamza (ء). And the last character is yāʼ (ي). So, if we want to match all characters of the Arabic alphabet we would write the expression as follows [ء-ي]. And this range will match also all the variations of Alif (أ) like أ, إ, ا, and ء.

Again, beware of the typing direciont. My advise is to write the brackets first, then change keyboard layout and put the cursor in the middle and type the Arabic characters.

[ء-ي]

إ ا أ ء ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي

[a-z] # For lowercase characters

a b c d e f g h i j k l m n o p q r s t u v W x y z

[A-Z] # For UPPERCASE characters

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Back to top