Pages

Monday, February 10, 2014

regexp

Postal Address 
[a-zA-Z\d\s\-\,\#\.\+]+

set address "
   Mr S Tan
   #200, Broadway Av
   WEST BEACH SA 5024  
   AUSTRALIA"

regexp {[a-zA-Z\d\s\-\,\#]+} $address new
puts $new


ZIP Code  
^\d{5,6}(?:[-\s]\d{4})?$


Date – accept date input in the mm/dd/yyyy or mm-dd-yyyy formats.
((0[1-9])|(1[0-2]))[\/-]((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))[\/-](\d{4})

set date "31/01/1000"
regexp {(0[1-9]|1[0-9]|2[0-9]|3[0-1])/(0[1-9]|1[0-2])/(\d{4})} $date match
puts $match


Email Address  
[a-zA-Z0-9_\.\+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+

set email "[email protected]"
regexp {[a-zA-Z0-9_]+.[0-9a-zA-Z_][email protected][a-z0-9A-Z_]+.[a-zA-Z_]+} $email match
puts $match


URL (Web domain)
https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}
https?\:\/\/(www\.)?youtu(\.)?be(\.com)?\/.*(\?v=|\/v\/)?[a-zA-Z0-9_\-]+

set site "https://www.facebook.com/"
regexp {(http|https)://www.[a-z]+.[a-z]+} $site match
puts $match

Character Limit 
[\w]{1,140}

Phone Numbers  
\+?\(?\d{2,4}\)?[\d\s-]{3,}

Price (with decimal)  
\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?

Complex Password – only accept a string that has 1 uppercase alphabet, 1 lowercase alphabet, 2 digits and 1 special character. Also the minimum allowed length is 8 characters.
(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9].*[0-9])(?=.*[^a-zA-Z0-9]).{8,}