-2

I have looked through several discussion and I am not seeing any helpful answers. I think my regex should be working. I have tested it http://regexr.com/ and http://www.regexplanet.com/advanced/java/index.html

It should be working. I am working on a simple validator POJO that validates strings before setting member variables.

private static final String emailReg = "(.+(@).\\w+\\..\\w+)";
private final Pattern emailPattern = Pattern.compile(emailReg);

And a method that gets called:

public boolean validateEmail( String Email ){

      Matcher m = emailPattern.matcher(Email);
      return m.matches();          
 }

This always returns false. The value passed can be any@any.any, but it always returns false. I am not at this point concerned about 'valid' emails, I just need to know if the regex string is properly assembled. From the tutorial pages for building regex'es it appears it is, but my program thinks otherwise.

CaptnJunks
  • 33
  • 10
  • 6
    Possible duplicate of [Java regex email](http://stackoverflow.com/questions/8204680/java-regex-email) – ItamarG3 Dec 28 '16 at 06:21
  • Please change the regex to `.+(@).\w+\..\w+` – Sumit Surana Dec 28 '16 at 06:23
  • 1
    @Sumit note that `\\` has to be escaped in a Java string literal. – Henry Dec 28 '16 at 06:25
  • @Henry recently after working on JS, totally missed the thought of string escape. I tested your code in my local machine, the code returned `true` for `any@any.any` – Sumit Surana Dec 28 '16 at 06:28
  • 1
    I cannot duplicate your problem. You may need to post a _complete_ program that demonstrates the error, because the problem may lie somewhere else. However, you may want to change `..` to `.` toward the end, because the way it's written, the dot can be followed by a non-word character (e.g. `any@any.$ny`), which might not be what you want. – ajb Dec 28 '16 at 06:38
  • Could you give some examples of strings that you are using in your program? – AntonH Dec 28 '16 at 06:41
  • I think I need to find where I might be loosing value. This is part of a JAVA web application using a JSF managed bean that has a POJO as the validator object. Finding where the String is getting stepped on might take a little work since Glassfish writes any System.out calls to the server log. – CaptnJunks Dec 28 '16 at 07:17
  • I may have figured it out, I am validating a form onchange, but am losing the client-side value somewhere in the process. If i directly enter in the email value as asdf@asdf.com it validates and returns true. This means my jsf backed bean is not getting the value onchange, only on submit, but I am preventing access to submit until after the entries are valid. I need to be more creative on my front end. Thanks for validating it should be working! :) – CaptnJunks Dec 28 '16 at 08:19

2 Answers2

0

I would recommend: \\w+@\\w+\\.\\w+.

This breaks down to: any amount of word characters "@" any amount of word characters "." any amount of word characters.

You can try it, "coder123@stackoverflow.com" would match, and "totally_invalid_email@%*.com" would not.

I believe that the reason why your regex is rejecting everything is because you seem to have extra .s in your code. If you want any@any.any, then ".+@.+\\..+" would be the proper regex in that case.

For more on java patterns, see https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Ian S.
  • 1,831
  • 9
  • 17
  • Why did you change the first `.+` to `\\w+`? Also, note that since he's using `matches`, the `^` and `$` in your pattern are redundant. Also, you don't need to escape `@`. Note that the two examples you give would also behave the same way on OP's regex. Your regex will reject some things that the OP's would accept, but this doesn't answer his question because he's saying that it's rejecting everything. – ajb Dec 28 '16 at 07:00
-1

I think there is some issue in your regular expression. Try this out.

package com.java.demo;

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

public class EmailValidator {

    public static void main(String[] args) {
        Pattern emailValidatorPattern = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$",
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = emailValidatorPattern.matcher("any@any.any");
        System.out.println(matcher.find());
    }

}

Hope this helps. Happy coding !

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63