newbie
newbie

Reputation: 23

regex for letter from any language does not work in php

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

Answers (2)

Emma
Emma

Reputation: 27723

This RegEx might help you to simply do that:

^[\pL_\w\d\s]+$

enter image description here

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_]+)\*

enter image description here

It creates two groups using ().

Upvotes: 1

Nick
Nick

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

Demo on 3v4l.org

Upvotes: 2

Related Questions