Feb 2, 2014

Useful Regex patterns

In web or desktop application forms reg-ex used to validate form vales. From this post you can find most used reg-exes for validations.
If you want a basic understanding of Reg-ex you can follow this post

Use online reg-ex tester to practice

 

For e-mails

  • ^\w[-.\w+&]*\w@\w[-.\w]*[a-zA-Z]{2,3}$ 
This accepts: 
me.myself@mysite.com 
you&me_developers@yoursite.lk 
123steve.me_contact@mysite.edu
     

For Telephone Numbers

  • ^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}$
This accepts:
(123)456-7890
1234567890
123-456-7890


For Numbers including Floating points

  • ^[-+]?\d*(\.\d+)?$
This accepts:
-20.456 
-.456 


For URLs

  • ^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}$
This accepts:
www.example.mysite.com
1234-sa5.au


For HTML tags

  • </?(\s*"[^"]*")*[^>]*>
 This accepts:
 <img title="displays >>" src="new.jpg">
</br>


For SQL validation
  •   /^(UPDATE|SELECT|INSERT INTO)(.*)(;)$/im
 This accepts:
 select * from table1