Java
 Computer >> コンピューター >  >> プログラミング >> Java

同様に有効な空白フィールドを含む電子メールを検証するJava正規表現プログラム


次の正規表現は、空白の入力を含む指定された電子メールIDと一致します-

^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})?$

どこで、

  • ^は文の先頭に一致します。

  • [a-zA-Z0-9 ._%+-]は、英語のアルファベットの1文字(両方の場合)、数字、 "+"、 "_"、 "。"、 ""、および、@記号の前の"-"に一致します。

  • +は、上記の文字セットが1回以上繰り返されることを示します。

  • @はそれ自体と一致します

  • [a-zA-Z0-9.-]は、英語のアルファベット(両方の場合)の1文字、数字、「。」に一致します。 @記号の後の「-」

  • \。[a-zA-Z]{2,6}「。」の後のメールドメインの2〜6文字

  • $は文の終わりを示します

例1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SampleTest {
   public static void main( String args[] ) {
      String regex = "^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})?$";
      //Reading input from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.nextLine();
      System.out.println("Enter your e-mail: ");
      String e_mail = sc.nextLine();
      System.out.println("Enter your age: ");
      int age = sc.nextInt();
      //Instantiating the Pattern class
      Pattern pattern = Pattern.compile(regex);
      //Instantiating the Matcher class
      Matcher matcher = pattern.matcher(e_mail);
      //verifying whether a match occurred
      if(matcher.find()) {
         System.out.println("e-mail value accepted");
      } else {
         System.out.println("e-mail not value accepted");
      }
   }
}

出力1

Enter your name:
krishna
Enter your e-mail:
Enter your age:
20
e-mail value accepted

出力2

Enter your name:
Rajeev
Enter your e-mail:
rajeev.123@gmail.com
Enter your age:
25
e-mail value accepted

例2

import java.util.Scanner;
public class Example {
   public static void main(String args[]) {
      //Reading String from user
      System.out.println("Enter email address: ");
      Scanner sc = new Scanner(System.in);
      String e_mail = sc.nextLine();
      //Regular expression
      String regex = "^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6})?$";
      boolean result = e_mail.matches(regex);
      if(result) {
         System.out.println("Valid match");
      } else {
         System.out.println("Invalid match");
      }
   }
}

出力1

Enter email address:
rajeev.123@gmail.com
Valid match

出力2

Enter email address:
Valid match

  1. Javaの正規表現\Dメタ文字

    部分表現/メタ文字「\ D 」は数字以外の数字と一致します。 例1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\\D";       String input = "This is sample text 1

  2. Pythonで正規表現を使用してメールIDを検証するにはどうすればよいですか?

    次のコードは、Pythonで正規表現を使用して特定のメールIDを検証します 例 import re s = 'manogna.neelam@tutorialspoint.com' match = re.search(r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b', s, re.I) print match.group() 出力 これにより、出力が得られます manogna.neelam@tutorialspoint.com