JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to validate email address using regular expression?


Description:

The below given example shows how to validate a email address using regular expression.


Code:
package com.java2novice.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyEmailValidate {

	private static Pattern emailNamePtrn = Pattern.compile(
	"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
	
	public static boolean validateEmailAddress(String userName){
		
		Matcher mtch = emailNamePtrn.matcher(userName);
		if(mtch.matches()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		System.out.println("Is '[email protected]' a valid email address? "
									+validateEmailAddress("[email protected]"));
		System.out.println("Is 'cric*7*[email protected]' a valid email address? "
									+validateEmailAddress("cric*7*[email protected]"));
		System.out.println("Is 'JAVA2NOVICE.gmail.com' a valid email address? "
									+validateEmailAddress("JAVA2NOVICE.gmail.com"));
	}
}

Output:
Is '[email protected]' a valid email address? true
Is 'cric*7*[email protected]' a valid email address? false
Is 'JAVA2NOVICE.gmail.com' a valid email address? false
<< Previous Program | Next Program >>

List Of Regular Expression Sample Programs:

  1. Simple regex pattern matching using string matches().
  2. Simple java regex using Pattern and Matcher classes.
  3. Java regex with case insensitive.
  4. How to validate IP address using regular expression?
  5. How to replace a pattern using regular expression in java?
  6. How to remove multiple spaces with a single space with in a string?
  7. How to validate user name using regular expression?
  8. How to validate email address using regular expression?
  9. How to validate password using regular expression?
  10. How to validate file extensions using regular expression in java?
  11. How to validate date format using regular expression in java?
  12. How to capture or extract a value(s) from text using regular expression in java?
  13. How to split a string using regular expression?
Knowledge Centre
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.