比如用户输入[email protected]是可以的,但输入[email protected]就要提示格式错误。怎么用正则表达式写非“.”结尾的字符串?非常感谢
解决方案 »
- PHP偶尔报Fatal error: Call to undefined method
- 高分请教,在线图片处理都是如何实现的!!!!
- PHP对SQL Server 2005中图片的插入和显示问题
- 一个文章采集的问题
- 暂时不想换成php5,但想把mysql4换成5...请问
- 用PHPLIB的Templatete类的人士有没有碰到这样的问题?
- discuz x2.5用户注册相关代码在哪个文件里?
- php 日期 月份
- PHP连接ACCESS..出现在错误!
- linux+mysql+apache+phpmyadmin 安装出现的问题,请高手解答
- 请教高手 急需解决我这个困惑已久的问题 关于 httpd.exe的问题
- IIS和Apache能在Win2K3下面共同搭建吗?
你问的问题本身就存在问题了,非“.”结尾的字符串 : [email protected]这个是以com结尾吧。给你两个
$pattern='/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/';$pattern='/^[^@]*<[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/';//两个,自己看着用哪个吧。
例如:
$patten = "/^\w+(?!=\.)$/is";
$wrongStr="[email protected]";$regex= '/\w[-.\w]*[^.]\@[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info)/';$matchCorrectStr = preg_match($regex, $correctStr);$matchWrongStr = preg_match($regex, $wrongStr);
echo "Result of match correct string:" .$matchCorrectStr;
echo "Result of match wrong string:" .$matchCorrectStr;结果:
Result of match correct string: 1
Result of match correct string: 0
解析:我们的这一个正则表达式可以拆分成三部分:
用户名 + @ + 主机名 1、用户名:/\w[-.\w]*[^.] 表示匹配除.结尾的任意ASCII字符
2、分隔符:\@ 匹配电子邮件的@分隔符
3、主机名:[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info) 点分的url主机地址可以包含数字字母或者-号
上面正则表达式摘抄自《精通正则表达式》 余晟 电子工业出版社出版