• 0

What is Regular Expressions?

Regular expressions is a technique or a language for telling a programming language or text editor how to parse a text. In a little bit more detail, it is used to represent a pattern using some special characters then this pattern is searched through given text and the matched text is extracted or undergone some operation i.e update, replace, delete.

regex-example

Here are the most common regular expressions build blocks:-

Expression Meaning
[a-z] Any lower case alphabets
[A-Z] Any Camel Case alphabets
[0-9] or \d Any number
[^a] Match any character which is not a
\s Any space character
. Any character
* Means zero or more of the character before it
? Means zero or one of the character before it
+ Means one or more of the character before it
{2,4} Means from two to four occurrences of the character before it
{2,} Means two or more occurrences the character before it
{,4} Means four or less occurrences of the character before it
{3} Means exactly three occurrences of the character before it
\ Escape any special character. For example \. means a literal dot.
^ Match start of a string
$ Match end of a string

The expressions above can be combined to form a pattern that validates any pattern. i.e. email, zip code, telephone number or credit cards.

The regular expression for validating an e-mail:-

The regular expression for validating a url :-

The regular expression for validating a telephone number:-

The regular expression for validating a visa credit card number:-

The regular expression for validating a master card number:-

The regular expression for validating US zip codes:-

The regular expression for matching IP address:-

The regular expression for matching a date in the format dd/mm/yyyy:-


Leave a Reply

%d bloggers like this: