Java USES regular expressions to determine whether a mailbox is properly formatted

  • 2020-04-01 04:02:24
  • OfStack

This article illustrates how Java USES regular expressions to determine if a mailbox is properly formatted. Share with you for your reference. The details are as follows:


import java.io.*;
public class CheckEmail 
{ 
 public static boolean checkEmail(String email)
  {//Validate the regular expression for the mailbox
   String format = "\p{Alpha}\w{2,15}[@][a-z0-9]{3,}[.]\p{Lower}{2,}";
   //P {Alpha}: the content is required, and is equivalent to the alphabetic character [p{Lower}p{Upper}]. 200896@163.com is not legal.
   //W {2,15}: 2~15 [a-za-z_0-9] characters; W {} content is required. dyh@152.com is legal.
   //[a-z0-9]{3,} : at least three [a-z0-9] characters,[] is required; dyh200896@16.com is not legal.
   //[.]:'.' must be selected; Such as: dyh200896@163com is not legal.
   //P {Lower}{2,} lowercase letters, more than two. dyh200896@163.c is not legal.
   if (email.matches(format))
    { 
     return true;//The mailbox name is valid and returns true
    }
   else
    {
     return false;//Invalid mailbox name returns false
    }
  } 
 public static void main(String[] args) throws Exception
 {
  String email = "cc**365@163.com"; //Mailboxes that need to be validated
   while(true)
  {
    email = new BufferedReader(new InputStreamReader(System.in)).readLine();
   if (CheckEmail.checkEmail(email))//Validation email
   {   
    System.out.println(email+"n Is the legal mailbox name. ");
   }
   else
   {
    System.out.println(email+"n Not a valid mailbox name. ");
   }
  }
 }
}

PS: here for you to provide two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
(link: http://tools.jb51.net/regex/javascript)

Regular expression online generation tool:
(link: http://tools.jb51.net/regex/create_reg)

I hope this article has been helpful to your Java programming.


Related articles: