30
Oct/11
Follow iNebium on twitter

A list of useful regexp

[30 Oct 11 at 09:16:54 - 0 comment(s)]

regexp or in other words regular expression is something you have to regularly use in your applications. I've decided to write this post as a placeholder for regexp I believe are common and useful such as regexp for email validation or some you might use to replace characters in a string. I might write some piece of java code but you can use these regular expression in many other languages.

1. regexp for validating email address

^[\\w\\-]+(\\.[\\w\\-]+)*@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$

But here is actually what I use to validate email address:
  public boolean isValidEmail(String input) {
    Pattern p = Pattern.compile("^\\.|^\\@");
    Matcher m = p.matcher(input);
    if (m.find())
      return false;
    
    p = Pattern.compile("^www\\.");
    m = p.matcher(input);
    
    if (m.find())
      return false;
    
    p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
    m = p.matcher(input);
    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);
    
    input = sb.toString();
    
    if(deletedIllegalChars)
      return false;
    
    return true;
  }

2. regexp to validate username of type test@test

[^@/]+@[^@/]+

3. regexp to replace multiple spaces

\\s+

4. regexp to replace any characters that is not alphanumeric

[^a-zA-Z0-9]


Conclusion

Don't hesitate to propose some more in the comments.

Add new comment





Notify me of follow up comments

Follow the RSS feed Follow the RSS feed

Recent Posts

How to run glxgears at full speed and not synchronized to vertical refresh?
You normally use glxgears to check performance of your graphic card but you get the same as your mon...
15 Nov 11
catalyst 11.10 / gnome-shell still not usable
In my post about catalyst 11.9, I wrote a rant about the AMD catalyst driver which after 6 months we...
01 Nov 11
A list of useful regexp
regexp or in other words regular expression is something you have to regularly use in your applicati...
30 Oct 11
catalyst 11.9 still doesn't work with gnome-shell!
Gnome 3.0 was released on the 6th of April 2011. At the time I write this post, Gnome 3.2 has been r...
29 Sep 11
3DES Encryption in Java and DES Encryption in Java
Here is a small post with just code to do 3DES (Triple DES) and DES Encryption in Java. You can simp...
13 Nov 10

Recent Comments

Unknown catalyst version 11.11 and still I have screen flickering, especially with something like banshee....
22 Jan 12 at 12:49:54
Javin @ Java Enum Example Java7 has several feature which developer would love one of them is fork-join framework which allows...
14 Jan 12 at 05:31:40
Javin @ Java Enum Example Java7 has several feature which developer would love one of them is fork-join framework which allows...
14 Jan 12 at 05:27:47
Vijay Hi,
This is the best tutorial I've come across so far to integrate maven with Eclipse even t...
27 Dec 11 at 11:07:13
inebium export vblank_mode=1...
25 Dec 11 at 01:19:14

Categories

All (22)
Android (2)
Java (6)
Linux (14)
Load Testing

Pricing

Sign Up For Free

+420 602 318 949 English / French (GMT+1)

Follow iNebium on twitter Follow us on Twitter

© iNebium 2009-2012 - All rights reserved