H Emad
H Emad

Reputation: 327

How use arabic letters with english letters and numbers in regular expression

I use below code to have something like this: " محمد125 hasan ". with a limit of being between 3 and 25 characters. but it doesn't work.

(^[ \d\p{Arabic}a-zA-Z0-9 ]{3,25}$)

Upvotes: 2

Views: 800

Answers (2)

Jan
Jan

Reputation: 43169

Plain and simple:

^[\d\p{L}\h]{3,25}$
# match the start (^), digits or any letter 
# and horizontal space three to 25 times
# end of the string/line ($)

In PHP this would be:

$string = 'محمد125 hasan ';
$regex = '~^[\d\p{L}\h]{3,25}$~';
if (preg_match($regex, $string, $match) {
    // do sth. useful here
}

See a demo on regex101.com.

Upvotes: 2

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

Just use \w with u modifier which means unicode

/^[ \d\p\wa-zA-Z0-9 ]{3,25}$/u

https://regex101.com/r/mC5uT3/1

Upvotes: 0

Related Questions