仅匹配一个 space 的阿拉伯语和英语字母数字

Matching only Arabic and English Alphanumeric with one space allowed only

我有一个论坛站点,目前正在处理最后一部分注册表,我想验证用户名。它应该只包含阿拉伯语和英语字母数字,单词之间最多 space。

我可以使用英文字母数字部分,但不能使用阿拉伯语和双 spaces。

我正在使用 preg_match() 函数将输入的用户名与 RegEX 相匹配。

我目前拥有的:

!preg_match('/\p{Arabic}/', $username) && !preg_match('/^[A-Za-z0-9]$/')
//this is currently inside and if statement, so if they both don't match then it is false.

您应该将 unicode 属性放在常规正则表达式中,因为这一切都可以用 1 个正则表达式来完成。您还需要量化该字符 class 否则您只允许使用 1 个字符。这个正则表达式应该可以做到。

^[\p{Arabic}a-zA-Z\p{N}]+\h?[\p{N}\p{Arabic}a-zA-Z]*$

在 PHP 中使用 u 修饰符,以便 unicode 按预期工作。

PHP 用法:

preg_match('/^[\p{Arabic}a-zA-Z\p{N}]+\h?[\p{N}\p{Arabic}a-zA-Z]*$/u', $string);

演示:https://regex101.com/r/fsRchS/2/