How do I run this regex in PHP?

前端 未结 2 1926
面向向阳花
面向向阳花 2021-01-25 00:31

I have this complex PHP I copied online:

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


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-25 01:22

    You are using a forward slash / as the delimiter. Since you're using the / character inside the expression, you need to escape it using a backslash character - so, instead of /, you'll need to write \/.

    The corrected version would be:

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

    Demo

提交回复
热议问题