如何在正则表达式中使用带有英文字母和数字的阿拉伯文字母

时间:2016-03-22 13:54:31

标签: php regex arabic farsi

我使用下面的代码来做这样的事情:“محمد125 hasan”。限制在3到25个字符之间。 但它不起作用。

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

2 个答案:

答案 0 :(得分:2)

简单明了:

^[\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 ($)

PHP中,这将是:

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

a demo on regex101.com

答案 1 :(得分:0)

只需使用\ {u修饰符,即unicode

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

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

相关问题