title: How to email validate with apostrophe and limit the number of dots after the @ symbol using Regular expression. (JAVA)
body: <p>Can someone help me please? I am not very familiar with regEx and I am trying to validate email addresses.
  The regEx and code that I have is:</p>
  
  <pre><code>public class TestRegExEmail {
  
  public static void main(final String[] args) {
  
      // List of valid URLs
      List&lt;String&gt; validValues = new ArrayList&lt;&gt;();
      validValues.add("wiliam@hotmail.com");
      validValues.add("wiliam.ferraciolli@hotmail.com");
      validValues.add("wiliam@hotmail.co.uk");
      validValues.add("wiliam.ferraciolli@hotmail.co.uk");
      validValues.add("wiliam_ferraciolli@hotmail.co.uk");
      validValues.add("wiliam'ferraciolli@hotmail.co.uk");
      validValues.add("wiliam334-1@mydomain.co.uk.me");
  
      // List on invalid URLs
      List&lt;String&gt; invalidValues = new ArrayList&lt;&gt;();
      invalidValues.add("wiliam.ferraciolli@hotmail.com.dodge.too.many");
      invalidValues.add("hwiliam@hotmail.com.otherdomain.uk.dodge");
      invalidValues.add("wiliam.ferraciolli@hotmail.com.com.com.com");
      invalidValues.add("wiliam.hotmail.com");
      invalidValues.add("wiliam..ferraciolli@hotmail.com");
      invalidValues.add("wiliam%ferraciolli.@hotmail.com");
      invalidValues.add("wiliam$ferraciolli.@hotmail.com"); 
      invalidValues.add("wiliam/ferraciolli.@hotmail.com");         
  
      // Pattern        
      String regex = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  
      Pattern pattern = Pattern.compile(regex);
  
      // print valid emails
      for (String s : validValues) {
          Matcher matcher = pattern.matcher(s);
          System.out.println(s + " // " + matcher.matches());
      }
  
      System.out.println();
      // print invalid emails
      for (String s : invalidValues) {
          Matcher matcher = pattern.matcher(s);
          System.out.println(s + " // " + matcher.matches());
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This regEx works fine but fails on emails with apostrophes. The other issue is that it would be ideal to allow only 3 dots after the @ symbol.
  Any suggestions would be appreciated.</p>
  
  <p>Regards</p>