I read about regexps, decided to bungle one to check whether this string is an email. It seems like it should work, but no.

Scanner s = new Scanner(System.in); while(true){ System.out.println("Input a valid email address."); Pattern email = Pattern.compile("([az[AZ]])+@([az[AZ]])+\Q.\E([az[AZ]])+"); Matcher matcher = email.matcher(s.nextLine()); if(matcher.matches()){ System.out.println("Valid"); }else System.out.println("Invalid"); } 

For all requests gives Invalid.

  • @Phynn For the code to be formatted, you must select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky ♦
  • Very much wrong email template. It is clear that under it half of all addresses in the internet will not work. - cy6erGn0m
  • Since nobody here reads the preface, I’ll remind you: I wrote this only to test my understanding of the work of regexps, and no more! - Phynn

2 answers 2

 \Q.\E 

change to:

 \\Q.\\E 

but in this case it is enough:

 \\. 
  • Thank; I first wrote one backslashes, forgot to screen). - Phynn

I would use the already checked regexp, so that the real addresses with numbers and domains not only of the second level passed the check.

 "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" 
  • The fact is that I am not going to use it, I just did a simple regexp to see how they work. But thanks for the answer - thanks :) A couple of questions on your answer: why did you put ^ before the first class, and number two - if I am not mistaken, it should be like that at the end - (\\. [A-zA-Z]) {2.4}. Well, since this is a proven regexp, it means that I don’t understand something). - Phynn