It blocks addresses that don't contain a single dot...
vax
@vax
Best posts made by vax
Latest posts made by vax
-
RE: How to validate an e-mail address the hard way?
-
RE: How to validate an e-mail address the hard way?
@Welbog said:
This makes me want to register www@gmail.com. Just 'cause.
You can do this, but not www.Welbog@gmail.com. His pattern includes a dot...
-
How to validate an e-mail address the hard way?
From the same developper as my previous post, a really nice Java method to validate (well... sort of) an e-mail address.
/** * Controle si l'email est correct * * @param email * mail a valider * @return true s'il est valide, false sinon */ public static boolean isEmailValid(String email) { if (email == null) return false; email = email.toLowerCase().trim(); if (!Tools.contains(email, "@") || (Tools.contains(email, " ") || !Tools.contains(email, "."))) { return false; } Pattern p = Pattern.compile("^\\.|^\\@"); Matcher m = p.matcher(email); if (m.find()) { // System.err.println("Email addresses don't start" + " with dots or @ signs."); return false; } // Checks for email addresses that start with // www. and prints a message if it does. p = Pattern.compile("^www\\."); m = p.matcher(email); if (m.find()) { // System.out.println("Email addresses don't start" + " with \"www.\", only web pages // do."); return false; } p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+"); m = p.matcher(email); StringBuffer sb = new StringBuffer(); boolean result = m.find(); boolean deletedIllegalChars = false; while (result) { deletedIllegalChars = true; m.appendReplacement(sb, ""); result = m.find(); } // Add the last segment of input to the new String m.appendTail(sb); email = sb.toString(); if (deletedIllegalChars) { // System.out.println("It contained incorrect characters" + " , such as spaces or // commas."); return false; } return true; }
Regular expressions are difficult, I know ;-)
Maybe it could become the reference implementation of the RFC -
RE: String contains
@bouk said:
@vax said:
I imagine this must be some kind of optimisation to avoid the use of String.indexOf or StringUtils.contains...
Some kind of optimisation... this code would wreak havoc with your performance if let loose on a source of 10k times "a", and a pattern of 5k "a" followed by a single "b". And, yes, such source/pattern pairs can be handled more efficiently, (most probably) by the methods referred to in other posts...
That's why I said "some kind"... It was ironic ;-)
-
String contains
I came across this code, written by a customer.
I imagine this must be some kind of optimisation to avoid the use of String.indexOf or StringUtils.contains...
/** * true is the source contains the pattern * * @param source * @param pattern * @return boolean */ public static boolean contains(final String source, final String pattern) { if (source == null || pattern == null) { return false; }
if (source.length() < pattern.length()) { return false; } // We use <= to manage the case : source = pattern for (int i = 0; i <= source.length() - pattern.length(); i++) { String tmp = source.substring(i, pattern.length() + i); if (tmp.compareTo(pattern) == 0) { return true; } } return false; }