1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 ().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.