douyu9159 2011-11-05 20:17
浏览 48
已采纳

正则表达式的区别?

I'm currently using this regex in my php script to validate email addresses:

preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);

Problem is, this specific regex is not reliable and often finds some false positives.

I was looking at regexlib.com to find a better one, but none of the ones I get from that site seem to find matches.

for example:

 preg_match_all("^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$", $string, $matches);

finds nothing, even though it should work according to regexlib. I've tried the top 10 regex terms on regexlib for email validation and none of them work. Am I doing something wrong?

Link to regexlib email category: http://regexlib.com/DisplayPatterns.aspx?cattabindex=0&categoryId=1

Thanks,

  • 写回答

2条回答 默认 最新

  • dongxuan2577 2011-11-05 20:18
    关注

    Your second regex fails because you need to add delimiters in PHP:

    "/([0-9a-zA-Z]...<snip>...[a-zA-Z]{2,9})/"
     ^                                      ^
     here                                here
    

    (I also removed the ^ and $ as Mario pointed out.)

    You could try a different approach though:

    1. Use a very simple regular expression to find anything that looks like it might be an e-mail address. It's OK to allow false-positives here, just make sure you don't miss any.
    2. For each email address candidate you found above, check it against a proper e-mail address validator to see if it is actually a valid e-mail address. Use this stage to remove the false positives.

    Related

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据