Php如何表达逐步增加两个,如何在PHP中“合并”这两个正则表达式?

我正在学习正规表达,所以请跟我一起轻松!

如果不以_(下划线)开头且仅包含单词字符(字母,数字和下划线本身),则用户名被视为有效:

namespace Gremo\ExtraValidationBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

use Symfony\Component\Validator\ConstraintValidator;

class UsernameValidator extends ConstraintValidator

{

public function validate($value, Constraint $constraint)

{

// Violation if username starts with underscore

if (preg_match('/^_', $value, $matches)) {

$this->context->addViolation($constraint->message);

return;

}

// Violation if username does not contain all word characters

if (!preg_match('/^\w+$/', $value, $matches)) {

$this->context->addViolation($constraint->message);

}

}

}

为了在一个正则表达式中合并它们,我尝试了以下内容:

^_+[^\w]+$

要读作:如果以下划线(最终多于一个)开头并且如果不允许至少一个字符(不是字母,数字或下划线),则添加违规.例如,不适用于“_test”.

你能帮我理解我错在哪里吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值