Ziyue Wang
Ziyue Wang

Reputation: 69

Regular expression java pattern doesn't validate?

The regular expression, "String regex = "[0-9a-z]+@[0-9a-z]+.+[0-9a-z]";" , is used for testing a email validation. Basically, im trying to make it so that an email will only match this patter if the email begins with a string of alphanumeric chars, followed by 1 @ symbol, followed by another string of alphanumeric chars, followed by 1 ., and then finally a string of alphanumeric chars. Whats failing is that when i enter an email without a string of alphanumerics after the last ., the program will still match with the regex string. How do i make it so that there MUST be another string of alphanumerics after the .? The whole code is:

import java.util.Scanner;
import java.util.regex.*;

public class Regex
{   
    public static void main (String[]args){

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your Email");
        String mail = input.nextLine();

        String regex = "[0-9a-z]+@[0-9a-z]+.+[0-9a-z]";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(mail);

        if(m.find()) {
            System.out.println("VALID");
        } else {
            System.out.println("INVALD");
        }
    }
}

Upvotes: 2

Views: 269

Answers (2)

DangerDan
DangerDan

Reputation: 529

A slightly better regex would include start of line ^ and end of line $ anchors.

Otherwise it will only need to match a single instance of a valid email in the string and it will pass. Also, instead of the plus sign to indicate 1 or more, you could restrict it to 2 to 4 characters by adding {2,4}. Without these in place something like

[email protected]@thisIsOdd.helloworld.anythingelse

will erroneously be valid.

String regex = "^[0-9a-z]+@([0-9a-z]+[.])+[0-9a-z]{2,4}$";

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

An unescaped . in the expression stands for any character. You need to use either \\., or [.] to match a literal dot.

String regex = "[0-9a-z]+@[0-9a-z]+[.]+[0-9a-z]";

The + after the dot means "one or more occurrences of the prior expression". Above, the "prior expression" is a single dot. To match multiple segments in the e-mail's domain address, you need to add parentheses:

String regex = "[0-9a-z]+@([0-9a-z]+[.])+[0-9a-z]+";

Upvotes: 4

Related Questions