# Common Regular Expressions Handbook

A regular expression is a sequence of characters used for parsing and manipulating strings.

Regular expressions are such an incredibly convenient tool, available across so many languages that most developers will learn them sooner or later. Fields of application of regular expressions ranges from validation to parsing/replacing strings, capturing specific strings and web scraping.

# Background Info on Regular Expressions

This is what Wikipedia has to say about them:

In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

In this article, we will go over some most common regular expressions which coders use to perform their tasks often. Before that we will understand what some symbols mean in regular expressions.

# Common Regular Expressions

## Digits

• Whole Numbers — `^\d+\$`
• Decimal Numbers — `^\d*\.\d+\$`
• Whole + Decimal Numbers — `^\d*(\.\d+)?\$`
• Negative, Positive Whole + Decimal Numbers — `^-?\d*(\.\d+)?\$`
• Whole + Decimal + Fractions — `[-]?[0-9]+(\.)?[0-9]*([\/][0-9]+[,.]?[0-9]*)*`

## Alphanumeric Characters

`^[a-zA-Z0-9]*\$`

## Email

`^([a-zA-Z0–9._%-]+@[a-zA-Z0–9.-]+\.[a-zA-Z]{2,6})*\$`

A strong password should have 1 lowercase letter, 1 uppercase letter, 1 number, 1 special character and be at least 8 characters long.

`(?=(.*[0-9]))(?=.*[\!@#\$%^&*()\\[\]{}\-_+=~`|:;"'<>,./?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}`

## URL

`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)`

`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\$`

## Dates

The elements are separated by ‘/’ or ‘ -’ or ‘.’. And also there is check for leap year.

`^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})\$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))\$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})\$`

## Time

• Time Format HH:MM 12-hour, optional leading 0
`^(0?[1-9]|1[0-2]):[0-5][0-9]\$`
• Time Format HH:MM 12-hour, optional leading 0, Meridiems (AM/PM)
`((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))`
• Time Format HH:MM 24-hour with leading 0
`^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\$`
• Time Format HH:MM 24-hour, optional leading 0
`^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\$`

## Date and Time in ISO-8601 Format

`^(?![+-]?\d{4,5}-?(?:\d{2}|W\d{2})T)(?:|(\d{4}|[+-]\d{5})-?(?:|(0\d|1[0-2])(?:|-?([0-2]\d|3[0-1]))|([0-2]\d{2}|3[0-5]\d|36[0-6])|W([0-4]\d|5[0-3])(?:|-?([1-7])))(?:(?!\d)|T(?=\d)))(?:|([01]\d|2[0-4])(?:|:?([0-5]\d)(?:|:?([0-5]\d)(?:|\.(\d{3})))(?:|[zZ]|([+-](?:[01]\d|2[0-4]))(?:|:?([0-5]\d)))))\$`

## Phone Numbers

`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(\d+))?\$`

## Slugs

`^[a-z0-9-]+\$`

## Hex Color Codes

`^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\$`

## HTML Tags

`<\/?[\w\s]*>|<.+[\W]>`

## US Zip Codes

`^[0-9]{5}(?:-[0-9]{4})?\$`

## India Zip Codes

`^[1-9][0-9]{5}\$`

## Morse Codes

Using `.` for a dot, `— `for a dash, separating letters by spaces and words by`|`.

`^[.-]{1,5}(?:[ \t|]+[.-]{1,5})*(?:[ \t|]+[.-]{1,5}(?:[ \t|]+[.-]{1,5})*)*\$`

## File Paths

`(\/)?[a-z0-9 _@\-^!#\$%&+={}.\/\\\[\]]+(\.[a-z]+)?\$`

# End

In this blog, we learnt about common regular expressions. This article can be used as a reference for handbook for regular expressions. Regular expressions are used by developers most often. So, I think it will be helpful. In case I encounter more regular expressions, I will add them here.

Written by

## rahul nayak

#### nayakrahul.github.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just \$5/month. Upgrade