22-Nov-2015 07:13

Regular expression for validating email in java

The second part, symbol means match the preceding zero or more times.

Mostly programming in Java, creating web based application with Spring Framework, JPA, etc.methods to match a string to be a valid email address based on the given regex.This examples also demonstrate the power of regular expression to validate an email address.Using regular expression makes it easier to validate data such as email.After the code you’ll see the meaning of the regular expression used in the code below.If you need help on Java programming you can hire me on Fiverr.

To sum up the discussion at you can reuse (keep in static variables) the compiled Pattern(s) and tell them to give you new Matchers when needed to validate those regex pattens against some string: import regex. Pattern; /** * Validation helpers */ public final class Validators { private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)* @[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; private static Pattern email_pattern; static { email_pattern = Pattern.compile(EMAIL_PATTERN); } /** * Check if e-mail is valid */ public static boolean is Valid Email(String email) { Matcher matcher = email_pattern.matcher(email); return matcher.matches(); } } For the Reg Ex pattern used, see the article at and the user comments (and useful links) posted there.

Regular expressions are used to search patterns of specific sequence of characters in Strings. - X, once or not at all X* - X, zero or more times X+ - X, one or more times X{n} - X, exactly n times X{n,m} - X, at least n times but no more than m times Boundary matchers ^ - beginning of line $ - end of line In Java some characters already have special meaning, so you will have to use ‘escape characters’ to properly construct your regular expression.

package org.kodejava.example.lang; public class Email Address Validation { public static final String EMAIL_REGEX = "^[\w-_\.+]*[\w-_\.]\@([\w]+\.)+[\w]+[\w]$"; public static void main(String[] args) { Email Address Validation validator = new Email Address Validation(); println("is Valid = " + Valid Email Address("[email protected]")); println("is Valid = " + Valid Email Address("[email protected]")); println("is Valid = " + Valid Email Address("[email protected]")); println("is Valid = " + Valid Email Address("user.sg")); } /** * Validates email address agains email regular expression.

* @param email an email address to check * @return true if email address is valid otherwise return false.

*/ protected boolean is Valid Email Address(String email) { return email.matches(EMAIL_REGEX); } } symbols means check the first character.

This the regex processor that the email address should start with a word character formed of alphanumeric value (a-z 0-9) or it can also be an hyphen, underscore, dot or a plus symbol.

Profile for regular expression for validating email in java:

regular expression for validating email in java-85

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “regular expression for validating email in java”