Learn Regular Expressions

Regular expressions are used to find specific patterns, to replace characters in a string using specific patterns, or usually used to validate inputs to match specific pattern.

Learn Regular Expressions

What is a Regular Expression? 

A regular expression is a sequence of characters and symbols or pattern of characters and symbols that is used to find, find and replace characters and validate a part of string. We will go through few simple examples to see how regular expressions play role in finding specific patterns , replace them or just validate a string.

Regular expressions are created using a specific syntax, Like /php/ where "/" at the start and end indicates that its a regular expression "php" indicates the pattern. When used this expression whether to search or replace it will look for exact word "php" in a string. The functions php uses for regular expressions are:


  • preg_match(): Performs a regular expression search, return true if matched and false if no match found.
  • preg_match_all(): Performs a global regular expression search, the difference if the first match is found it keeps searching for given pattern until the end of string.
  • preg_replace(): Performs a regular expression search and replace for a given pattern. Pattern can be passed as a string or array of strings.
  • preg_replace_callback(): Performs a regular expression search and replace using a callback function.
For detailed usage of these function refer to PHP's PCRE Functions. For example if we want to search for a word "php" in this string "this is a string in php", We will do it like so:
preg_match("/php/", "this is a string in php");

Regular Expressions Examples:

Expression Definition Example Sample
^ Start of string /^abc/ abcword
$ End of string /abc$/ wordabc
\d Any digit /word\d/ word5
\w Any alphanumeric or underscore /123\w/ 123Z
[0-9] Numbers from 0 to 9 /word[0-9]/ word1
[a-z] Letters from a to z /123[a-z]/ 123a
[A-Z] Letters from A to Z /123[A-Z]/ 123W
+ Minimum one occurrence /\d+/ 123456
* Zero or more occurrences /\d+A*/ 123AAA or 123
? Zero or one occurrence /\d+ab?c/ 123abc or 123ac
. Any character that is not a line break /x.z/ xyz
{3} Exactly three occurrences /a{3}/ aaa
{2,4} Minimum two and maximum four occurrences /a{2,4}/ aa
{2,} Two or more occurrences /\w{2,}/ word_word

Mostly Used Regular Expressions: 

Verify only digits with regular expression:
Expression Sample Result
/^[0-9]+$/ 123456 Pass
123abc Fail

Verify only alphabets with regular expression:
Expression Sample Result
/^[a-zA-Z]+$/ abcXYZ Pass
abcXYZ12 Fail

Verify email address with regular expression:
Expression Sample Result
/^[\w\-\.]+\@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/ [email protected] Pass
user@domain Fail
userdomain.com Fail

Verify url with regular expression:
Expression Sample Result
/^((https?|ftp):\/\/)(www\.)?[\w\-]+\.[a-z]{2,4}\/?$/ https://domain.com Pass
http://domain.com Pass
http://www.domain.com Pass
www.domain.com Fail