Reputation: 23
I am using following regex expression to allow letters from any language, numbers, underscore and spaces.
^[\p{L}0-9 _]\*[\p{L}0-9][\p{L}0-9 _]\*$
It works perfectly fine in online regex tester tools but not in my PHP code. For example it will not match with say any Russian word "Привет".
Any idea why?
PHP version: PHP 7.1.16
Upvotes: 1
Views: 99
Reputation: 27723
This RegEx might help you to simply do that:
^[\pL_\w\d\s]+$
You may simplify it more and it might still work such as:
[\pL_\d\s]+
which you could remove \w for words, and start ^ and end $ chars.
Based on your RegEx, I would think that this RegEx might be the one it might work:
([\pL\d\s_]+)\*([\pL\d]+[\pL\d\s_]+)\*
It creates two groups using ()
.
Upvotes: 1
Reputation: 147146
You have a couple of issues with your regex. Firstly it will only match 3 characters without the addition of a *
or +
after one of the character sets. Secondly, to match unicode in PHP, you need to use the u
modifier on your regex. Try this instead:
$str = 'Привет';
$regex = '/^[\p{L}0-9 _][\p{L}0-9]+[\p{L}0-9 _]$/u';
echo preg_match($regex, $str);
Output:
1
Upvotes: 2