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 create a self signed certificate?
You need a self signed certificate for your website over https, this post is here to take you throug...
26 Apr 12
To not increase PCM value when increasing volume with pulseaudio
When you use pulseaudio and you increase the volume, it will increase the front volume but also the ...
17 Apr 12
Skype: fix crackling sound
If skype on linux gives you crackling sound and you use pulseaudio, here is the solution to fix it. ...
17 Apr 12
Install grub when booting from live CD or usb
You need to re-install grub on your drive when booting from usb or live CD. This post is made for yo...
12 Apr 12
Git: common and useful commands
I use git and literally love it but I have one problem with it, I often forget how to do things mean...
22 Mar 12

Recent Comments

vvy2000 ubuntu need root for executing this procedure.
sudo -s to get root privileges. ...
04 May 12 at 09:40:53
Danilo Cesar echo 3 | sudo tee /proc/sys/vm/drop_caches ...
16 Mar 12 at 09:32:42
inebium it's not a method, it's the constructor of the class...
05 Mar 12 at 03:22:42
Unknown method Encrypter has no return type .... ? ...
05 Mar 12 at 02:28:05
Unknown catalyst version 11.11 and still I have screen flickering, especially with something like banshee....
22 Jan 12 at 12:49:54

Categories

All (27)
Android (2)
Java (6)
Linux (18)
Misc (1)
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