Learn Regular Expressions

Regular expressions also shortened as regex are used to find specific patterns, to replace characters in a string using specific patterns, or mostly used to validate inputs to match specific pattern.

Learn Regular Expressions

What is a Regular Expression?

A regex (regular expression) is a sequence or pattern of characters used to find 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 used in many applications like notepad++, Microsoft applications such as Microsoft Word. Besides regex usage in application, it is also used in web development when developing web application.

How to write a regular expression?

Regular expressions are created using a special syntax, In PHP and Javascript "/" at the start and end indicates that its a regular expression. To use a regex properly you need to have at least basic understanding of syntax. Lets take a look at basic special characters and quantifiers mostly used to write a regex (regular expression).

  1. ^ regex quantifier indicates the start of string or line in pattern. For example "^hello" would mean the string or line should start with "hello".
  2. $ regex quantifier indicates the end of string or line in pattern. For example "bye$" would mean the string or line must end with "bye".
  3. * regex quantifier indicates the zero or more occurrence of preceding character or group of characters. For example "123*A" would mean the string has "123" number and "A" could appear any number of times or not at all.
  4. + regex quantifier indicates the preceding character or group of characters occurs at least one time and could be more than one time. For example "abc+" would mean the string should have "ab" and "c" at least one time or more. So "abc" and "abccc" both will pass the match but only "ab" will not.
  5. ? regex quantifier indicates zero or none occurrence of the preceding character or group of characters. For example "https?" would mean the the "s" at that position can occur only once or can be missing. So "htpp" and "https" both will pass the match but "httpss" will not.
  6. () regex captures a group of characters. For example "(www\.)" will mean the string should be "www." as a whole group. Regex groups are also used for references in following patterns.
  7. [] regex indicates set of characters or any of the characters inside brackets. For example "123[a-zA-z]" will mean string should be "123" and any alphabet either small case or capital case. So "123H" will be a correct match.
  8. {} regex limits the occurrence of preceding character or group of characters to specific number or between a specific range. For example "a{2,4}" will mean the letter "a" should appear between two or four times. In this case "aa", "aaa" and "aaaa" will be a valid match.

Regular Expression Examples: 

The following table shows the different quantifiers and logical syntax of regular expressions. This table can be used as a quick reference for writing regular expressions.

Expression Definition Example Sample
^ Start of string/line /^abc/ abcword
$ End of string/line /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

Commonly Used Regular Expressions: 

For a developer there certain regex patterns mostly or commonly used to validate certain pattern or find and replace certain parts of strings. The following regular expressions can be used in PHP as well as JavaScript.

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