简体   繁体   中英

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. 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('@'); if(i == -1) return; String address = str.substring(0, at - 1);

  2. Find domain

    if(str.substring(at).indexOf('.') == -1) return; String domain = str.substring(at + 1)

  3. Validate for incorrect symbols

  4. Validate (send SMTP to server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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