比如用户输入[email protected]是可以的,但输入[email protected]就要提示格式错误。怎么用正则表达式写非“.”结尾的字符串?非常感谢

解决方案 »

  1.   

    就是验证email吧?
    你问的问题本身就存在问题了,非“.”结尾的字符串 : [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])?>$/';//两个,自己看着用哪个吧。
      

  2.   

    /^[a-zA-Z0-9_]*?[^\.]$/请自行验证,-_-||
      

  3.   

    非.结尾这种需求,可以考虑用否定环视。
    例如:
    $patten = "/^\w+(?!=\.)$/is";
      

  4.   

    $correctStr="[email protected]";
    $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主机地址可以包含数字字母或者-号
    上面正则表达式摘抄自《精通正则表达式》 余晟 电子工业出版社出版