Useful regex's to verify email, alphanumeric, numeric (to ensure that an entire string matches)

Useful regex's to verify email, alphanumeric, numeric (to ensure that an entire string matches) admin 27 August, 2009 - 14:54

Regex is a black art, with the usual rule being the smaller the better.

The following are a basic collection that I use quite often:

  • Regex to validate email - i.e. of the form x@a.tld
    ^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\.\-]+$
    
  • Regex to match only alphanumeric
    ^([a-zA-Z0-9]+)$
    
  • Regex to match only alpha
    ^([a-zA-Z]+)$
    
  • Regex to match only numeric
    ^([0-9]+)$
    
  • Regex to match only numeric (decimal or floating point)
    ^([0-9.]+)$
    
  • Regex to match only numeric (international)
    ^([0-9.,]+)$