Regular Expression Arabic characters and numbers only
RegexUnicodeString MatchingRegex Problem Overview
I want Regular Expression to accept only Arabic characters, Spaces and Numbers.
Numbers are not required to be in Arabic.
I found the following expression:
^[\u0621-\u064A]+$
which accepts only only Arabic characters while I need Arabic characters, Spaces and Numbers.
Regex Solutions
Solution 1 - Regex
Just add 1-9
(in Unicode format) to your character-class:
^[\u0621-\u064A0-9 ]+$
OR add \u0660-\u0669
to the character-class which is the range of Arabic numbers :
^[\u0621-\u064A\u0660-\u0669 ]+$
Solution 2 - Regex
You can use:
^[\u0621-\u064A\s\p{N}]+$
\p{N}
will match any unicode numeric digit.
To match only ASCII digit use:
^[\u0621-\u064A\s0-9]+$
EDIT: Better to use this regex:
^[\p{Arabic}\s\p{N}]+$
Solution 3 - Regex
you can use [ء-ي] it worked for me in javascript Jquery forme.validate rules
for my example I want to force user to insert 3 characters
[a-zA-Zء-ي]
Solution 4 - Regex
Solution 5 - Regex
With a lot of try and edit i got this for Persian names:
[گچپژیلفقهمو ء-ي]+$
Solution 6 - Regex
^[\u0621-\u064Aa-zA-Z\d\-_\s]+$
This regex must accept Arabic letters,English letters, spaces and numbers
Solution 7 - Regex
Simple, use this code:
^[-ۿ]+$
This works for Arabic/Persian even numbers.
Solution 8 - Regex
function HasArabicCharacters(string text)
{
var regex = new RegExp(
"[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");
return regex.test(text);
}
Solution 9 - Regex
To allow Arabic + English Letters with min&max allowed number of characters in a field, try this, tested 100%:
^[\u0621-\u064A\u0660-\u0669a-zA-Z\-_\s]{4,35}$
A- Arabic English letters Allowed.
B- Numbers not allowed.
C- {4,35} means the Min,Max characters allowed.
Update: On submit: Accepted English words with spaces, but the Arabic words with spaces could not be submitted!
Solution 10 - Regex
Regex for English and Arabic Numbers only
function HasArabicEnglishNumbers(text)
{
var regex = new RegExp(
"^[\u0621-\u064A0-9]|[\u0621-\u064A\u0660-\u0669]+$");
return regex.test(text);
}
Solution 11 - Regex
@Pattern(regexp = "^[\\p{InArabic}\\s]+$")
Accept arabic digit and character
Solution 12 - Regex
In PHP, use this:
preg_replace("/\p{Arabic}/u", 'x', 'abc123ابت');// will replace arabic letters with "x".
Note: For \p{Arabic}
to match arabic letters, you need to pass u
modifier (for unicode) at the end.
Solution 13 - Regex
The posts above include much more than arabic (MSA) characters, it includes persian, urdu, quranic symbols, and some other symbols. The arabic MSA characters are only (see Arabic Unicode)
[\u0621-\u063A\u0641-\u0652]
Solution 14 - Regex
I always use these to control user input in my apps
public static Regex IntegerString => new(@"^[\s\da-zA-Zء-ي]+[^\.]*$");
public static Regex String => new(@"^[\sa-zA-Zء-ي]*$");
public static Regex Email => new(@"^[\d\@\.a-z]*$");
public static Regex Phone => new(@"^[\d\s\(\)\-\+]+[^\.]*$");
public static Regex Address => new(@"^[\s\d\.\,\،\-a-zA-Zء-ي]*$");
public static Regex Integer => new(@"^[\d]+[^\.]*$");
public static Regex Double => new(@"^[\d\.]*$");
Solution 15 - Regex
This is useful example
public class Test {
public static void main(String[] args) {
String thai = "1ประเทศไทย1ประเทศไทย";
String arabic = "1عربي1عربي";
//correct inputs
System.out.println(thai.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.THAI.toString() + "}*]*"));
System.out.println(arabic.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.ARABIC.toString() + "}*]*"));
//incorrect inputs
System.out.println(arabic.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.THAI.toString() + "}*]*"));
System.out.println(thai.matches("[[0-9]*\\p{In" + Character.UnicodeBlock.ARABIC.toString() + "}*]*"));
}
}
Solution 16 - Regex
[\p{IsArabic}-[\D]]
An Arabic character that is not a non-digit