27
Jul
10

Here are some commonly used regular expressions. Most of them are applicable for PHP, JavaScript, Perl, Python and Java.

HTTP URL

/*
Matches: https://tutorboy.com, http://tutorboy.com:8080/profile.html
Nonmatches: ftp://tutorboy.com, ftp://tutorboy.com/
*/
Expression : /(https?):\/\/([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})(:\d{1,4})?([-\w\/#~:.?+=&%@~]*)/

Email

/*
Matches: mail@mytutorboy.com, email@my-mail.com, info@mymail.mysite.net
Nonmatches: .@example.com, mittu@i-.com, ilov@example.a
*/
Expression : /^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/

Valid HTML Hex code

/*
Matches: #fff, #1a1, #996633
Nonmatches: #ff, FFFFFF
*/
Expression : /^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?$/

Match date: MM/DD/YYYY HH:MM:SS

/*
Matches: 04/30/1978 20:45:38
Nonmatches: 4/30/1978 20:45:38, 4/30/78
*/
Expression : /^\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d$/

U.S. zip code

/*
Matches: 94941-3232, 10024
Nonmatches: 949413232
*/
Expression : /^\d{5}(-\d{4})?$/

U.S. currency

/*
Matches: $20, $15,000.01
Nonmatches: $1.001, $.99
*/
Expression : /^\$\(d{1,3}(\,\d{3})*|\d+)(\.\d{2})?$/

Dotted Quad IP address

/*
Matches: 127.0.0.1, 224.22.5.110
Nonmatches: 127.1
*/
Expression : /^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/

MAC address

/*
Matches: 01:23:45:67:89:ab
Nonmatches: 01:23:45, 0123456789ab
*/
Expression : /^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$/

Type/format specifiers

/*
Matches: my number %d, Hello %s!!, %f, %’-20s
Nonmatches: my number %t, hello %p
*/
Expression : #(%[+-]?(([ 0]?)|('.)))-?(d*)(.d*)?[%bcdeufFosxX]#
  • del.icio.us
  • Digg
  • Facebook
  • LinkedIn
  • Google Bookmarks
  • Twitter
  • MySpace
  • Mixx
  • Reddit
  • DZone
  • Sphinn
  • StumbleUpon
  • Blogplay
  • Diigo
  • Live
  • MSN Reporter
  • Technorati
  • NewsVine
  • Yahoo! Bookmarks
  • Netvibes
  • Design Float
  • Add to favorites
  • email
  • Yahoo! Buzz
  • Gwar
  • FriendFeed
  • Tumblr
  • Print
  • PDF
  • RSS

About Midhun Devasia

I am working as a Technical Manager for a Web Development company at Cochin, India. Completed my B.Tech Computer Science and Engineering from SNGCE. I'm the lead developer of Tutorboy.com.
This entry was posted in Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>