Sunday, December 3, 2017

Learn Regular Expressions

Regular expressions are used to find patterns or find and replace patterns in strings 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 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}$/ user@domain.com 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