正则表达式仅限于阿拉伯字符和数字

我希望正则表达式只接受 阿拉伯文字空格数字

数字不需要是阿拉伯语。

我发现了下面这句话:

^[\u0621-\u064A]+$

它只接受阿拉伯字符,而我需要阿拉伯字符,空格和数字。

87258 次浏览

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}]+$

RegEx Demo

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 ]+$

use this

[\u0600-\u06FF]

it worked for me on visual studio

enter image description here

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ء-ي]

function HasArabicCharacters(string text)


{


var regex = new RegExp(


"[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]");


return regex.test(text);
}
[\p{IsArabic}-[\D]]

An Arabic character that is not a non-digit

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.

With a lot of try and edit i got this for Persian names:

[گچپژیلفقهمو ء-ي]+$

Simple, use this code:

^[؀-ۿ]+$

This works for Arabic/Persian even numbers.

^[\u0621-\u064Aa-zA-Z\d\-_\s]+$

This regex must accept Arabic letters,English letters, spaces and numbers

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]

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!

All cases tested

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);
}
@Pattern(regexp = "^[\\p{InArabic}\\s]+$")

Accept arabic digit and character

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\.]*$");

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() + "}*]*"));
    

}

}

This one allows Arabic letters, Arabic numbers and English numbers

var arabic = RegExp("^[\u0621-\u064A\u0660-\u0669 1-9]+\$");