简体   繁体   English

电子邮件验证 - 我的正则表达式代码有什么问题?

[英]Email validation - What is wrong with my regex code?

I'm trying to validate an email address using preg_match.. 我正在尝试使用preg_match验证电子邮件地址..

But i'm getting this error.. 但是我收到了这个错误..

Warning: preg_match(): Unknown modifier '+' 警告:preg_match():未知修饰符'+'

This is my code 这是我的代码

preg_match("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", $email, $matches);

Can someone tell me what is wrong with my regex? 有人能告诉我我的正则表达式有什么问题吗?

Thanks 谢谢

You need to put delimiters around the regex when you use preg_match . 使用preg_match时,需要在正则表达式周围放置分隔符。 The standard is / . 标准是/ If you use the delimiter in the expression, you have to escape it. 如果在表达式中使用分隔符,则必须将其转义。

preg_match("/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/", $email, $matches);

You could also consider using 你也可以考虑使用

filter_var($email, FILTER_VALIDATE_EMAIL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM