user311509
user311509

Reputation: 2866

Match Arabic/English Alphanumeric using Regex

I would like to have a regular expression that matches:

Any order.


I tried varies solution but couldn't solve it.

Here is what i have now:

preg_match('@^([^\W_]*\s){0,3}[^\W_]*$@', $username)

The above expression allows:

Upvotes: 1

Views: 2829

Answers (2)

Rok Kralj
Rok Kralj

Reputation: 48775

You can check if your Regex flavour supports this \p{Arabic} or \p{InArabic}.

Also experiment with mb_ereg_match() function: https://www.php.net/manual/en/function.mb-ereg-match.php

If that doesn't work, there is no other option than explicitly writing all arabic characters into the expression. Messy, but does the work.

Since you are using php, you can first list all arabic characters into a string variable and then add that variable to regex, for the code manageability's sake.

Upvotes: 3

pho
pho

Reputation: 25500

I don't know about arabic characters, but the following regexp should match the others

([a-zA-Z0-9]{1,})\s{0,3}_{0,4}

This will match (Alphanumeric)(0-3 spaces)(0-4 underscores)

If there are more than 4 underscores, the last ones will be omitted If there are more than 3 spaces then the part after the 3 spaces will be ignored.

EDIT: For arabic letters: First declare a string containing all arabic letters so you'll have

$arabic='all_arabic_letters';

Then your regexp string will be

$regex='[' . $arabic . ']{1,}([a-zA-Z0-9]{1,})\s{0,3}_{0,4}';

And match it as follows:

preg_match($regex, $username);

Upvotes: 0

Related Questions