Skip Navigation Links



Some useful PHP, Perl, and JavaScript Regular Expressions

Author : mohit      Blog :Mohit Kumar      Date: 6/13/2012 9:09:31 AM


A regular expression, also called regex or regexp for short, is simply a piece of code that matches a pattern. Mastering regular expressions can be a difficult chore, and if you don’t need them all of the time, the syntax is tricky enough to make the task frustrating or slow as you will constantly need to use a reference sheet.

In order to save you time, I’ve compiled a list of PHP, Perl, and JavaScript regular expressions for common use cases that have been tested and are ready to go. This isn’t a regular expression tutorial or even a reference; you can think of it more as a cheatsheet for when you just need the regex but don’t want to put a lot of time into relearning regular expressions.

 

Perl and PHP Regular Expressions

PHP regexes are based on the PCRE (Perl-Compatible Regular Expressions), so any regexp that works for one should be compatible with the other or any other language that makes use of the PCRE format. Here are some commonly needed regular expressions for both PHP and Perl. Each regex will be in string format and will include delimiters.

All Major Credit Cards

This regular expression will validate all major credit cards: American Express (Amex), Discover, Mastercard, and Visa.

  1. //All major credit cards regex
  2. ‘/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/’

Alpha-Numeric Characters

Test for alpha-numeric characters with this regexp.

  1. //Alpha-numeric characters only
  2. ‘/^[a-zA-Z0-9]*$/’

Alpha-Numeric Characters With Spaces

Test for alpha-numeric characters and spaces with this regexp.

  1. //Alpha-numeric characters with spaces only
  2. ‘/^[a-zA-Z0-9 ]*$/’

Alphabetic Characters

This regex will test for alphabetic characters only (upper and lowercase).

  1. //Alphabetic characters only
  2. ‘/^[a-zA-Z]*$/’

 

Date (MM/DD/YYYY)

Validate the calendar date in MM/DD/YYYY format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099.

  1. //Date (MM/DD/YYYY)
  2. ‘/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/’

Date (YYYY/MM/DD)

Validate the calendar date in YYYY/MM/DD format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099.

  1. //Date (YYYY/MM/DD)
  2. ‘#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#’

Digits

This regex will test for digits (whole numbers).

  1. //Digits only
  2. ‘/^[0-9]*$/’

 

Emails

This email regex is not fully RFC5322-compliant, but it will validate most common email address formats correctly.

  1. //Email regex
  2. ‘/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/’

IP Addresses

Test IP Addresses with this regular expression.

  1. //IP address regex
  2. ‘/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/’

Lowercase Alphabetic Characters

This regex will test for lowercase letters.

  1. //Lowercase letters only
  2. ‘/^([a-z])*$/’

 

Uppercase Alphabetic Characters

This regex will test for uppercase letters.

  1. //Uppercase letters only
  2. ‘/^([A-Z])*$/’

URLs

This URL regex will validate most common URL formats correctly.

  1. //URL regex
  2. ‘/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/’

 

Bloggers

active bloggers in the last 24 hrs. Number shown in the bracket represents number of posts published in past 24 hrs,


other authors(106)

Fidarose Isha(3)

ravi dabas(3)

Srinivasan Sampathkumar(3)

AMJAD KHAN(2)

Deepak(2)

MUNZ TDT(2)

Rakesh HP(2)

Raksha(2)

RWABhagidari.blogspot.com(2)

Sameena Prathap(2)

The Guy in the Hat ™(2)

vskesavarao(2)

Aarthi(1)

Aathira Nair(1)

Abha Iyengar(1)

Abhishek Mukherjee(1)

Abraham Tharakan(1)

admin(1)

Always Happy(1)

Anil(1)

anouradha bakshi(1)

Anu Varma(1)

Archana Kumar(1)

Beyond(1)

Bhushavali N(1)

Chandrika Shubham(1)

churumuri(1)

churumuri(1)

CM(1)

dawdayogesh(1)

Debolina Raja Gupta(1)

Devi(1)

Dimple Maheshwari(1)

Disha(1)

Dr.K.P.R.RAJA(1)

Everything(1)

Garfield Dsouza(1)

Govind Kumar(1)

Harimohan(1)

Harini Padmanabhan(1)

harish p i(1)

Hemu(1)

iBeingMe(1)

Indrani(1)

Iti(1)

k(1)

Kalamwali Bai(1)

Krithi Karthi(1)

Kunal Singh(1)

Lluvia....(1)

Madhavi Madhurakavi(1)

Meena(1)

Megha Sarin(1)

mêlée(1)

mervin anto(1)

Mohd Salim(1)

Mythreyi(1)

Nandana(1)

Neelam Dadhwal(1)

Neeraja(1)

NG(1)

Nisheeth Ranjan(1)

Nivedita Thadani(1)

Nivedita Thadani(1)

Nona(1)

Nupur(1)

palash ranjan khound(1)

Pari Vasisht(1)

parth joshi(1)

Pheno Menon(1)

pinksocks(1)

Pradeep Chakraborty(1)

Pranshu(1)

Prasanta Bora(1)

Preety(1)

Priyadarshi Mishra(1)

R-A-J(1)

Raja(1)

rajkumar r(1)

Rams(1)

Ray Titus(1)

Ritesh Agarwal(1)

Ritu(1)

rm(1)

RNA Corp(1)

Sabina Fatima Hussain(1)

Sameena Prathap(1)

Sampada(1)

Santhosh Sivarajan(1)

Saravanan Sivaraman(1)

sayedkhadri(1)

Shankha(1)

Shankha(1)

Shobha(1)

Shobhaa De(1)

sidhu352(1)

Smi..(1)

Sneha Sharma(1)

Sneo(1)

Sriram Khé(1)

Sudharsan Narayanan(1)

Susan Deborah(1)

swapnil kochetaa(1)

Tarang Sinha(1)

Team G Square(1)

Umasree(1)

vanya(1)

Vidya Sury(1)

Vikram Karve(1)

vishesh unni raghunathan(1)


garland of Languages of India
an amalgamation of the diversified traditions
gracefully presented with novelty
http://www.haaram.com