简体   繁体   English

什么是经过优化的正则表达式以匹配Java中的电子邮件模式

[英]What is the optimized regex to match the email pattern in java

I am using the below regex in my program for matching the email pattern which is working fine. 我在程序中使用以下正则表达式来匹配正常工作的电子邮件模式。 Pattern : 模式:

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

It matching all the emails correctly. 它正确匹配所有电子邮件。 But now my use case is to apply above email regex for content which contains very large junk values. 但是现在,我的用例是在电子邮件正则表达式之上应用包含非常大的垃圾值的内容。 Ex: 30K continues alphanumeric characters without any empty space. 例如:30K继续字母数字字符,没有任何空格。 In such cases, code take more very long time to process. 在这种情况下,代码将花费更多的时间。 Ex : more than a minutes , where normal content (without such junk content) takes less than a second . 例如: 超过一分钟 ,正常内容(不包含垃圾内容)的时间不到一秒钟

Couple of clarifications: 几个说明:

  1. Why system taking long time while matching above regex for large junk characters 为什么系统在匹配正则表达式上的大型垃圾字符时花很长时间
  2. What would be the optimized regex to overcome the delay 什么是克服延迟的优化正则表达式

Not with regex. 不适用于正则表达式。

  1. Find character '@' at line 在行中找到字符“ @”

    int at = str.indexOf('@'); int = str.indexOf('@'); if(i == -1) return; if(i == -1)返回; String address = str.substring(0, at - 1); 字符串地址= str.substring(0,at-1);

  2. Find domain 查找域名

    if(str.substring(at).indexOf('.') == -1) return; if(str.substring(at).indexOf('。')== -1)返回; String domain = str.substring(at + 1) 字符串域= str.substring(at + 1)

  3. Validate for incorrect symbols 验证不正确的符号

  4. Validate (send SMTP to server) 验证(将SMTP发送到服务器)

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

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