Regular Expressions- Regex with Examples. (In Short)

Image Credits: The Net Ninja

Regular Expressions, or Regex, is all about pattern matching where everything is considered to be characters. Using regex is similar to picking selected drops from the ocean of water. Eg: In case where we need to find all strings that match a certain pattern, we could use regex. A simple application that we all might have used is Find (CTRL + F) command.

This article was written as my Internship Task at geeksgod.

Table of Contents


Here pattern refers to what we are searching and strings found represent matching results.

Matching Generic Pattern (Anywhere in the string)

Both “example123abc” , ” var=’123a’ “match pattern “123a

Match Specific Pattern (At certain positions)

Single wildcard character

To find all three(or n) letter word ending with full-stop:

Pattern: “…\.” (Here, the starting three dots/ period indicate single wild card character and “\.” is to identify an actual “.” )

The following strings match the specified pattern: “bat.“,”nod.“,”12a.”,”x?=.

Certain Characters only

Pattern: [abc]an

Matching Strings: aan, ban,can

Non-matching Strings: dan, fan, pan, etc. All words with pattern “.an” except for 3 matching words.

Excluding specific characters

Pattern: [^dpt]an

Matching Strings: All words of pattern “.an” except for 3 non-matching words

Non-matching Strings: dan, pan, tan

Character ranges

Example: Starting With

Pattern: [A-Z0–9]..

Matching Strings: All 3 letter words of pattern starting with either any letter between A-Z or digits 0–9.

Non-matching Strings: dxe, f1A, hgkeiyuyoi

Example : Starting With, having second char as…

More specific example could be : To match words of any length starting with small alphabet , second character being a digit and following characters could be any.

Pattern: [a-z][0–9]

Matching Strings: a12, b3h,z5nhgkhg

Non-matching Strings: dx1, A2fhkh, 7asf


Exactly n times

Pattern: z{3} Any word having ‘z’ 3 times

Matching Strings: a12, b3h,z5nhgkhg

Non-matching Strings: dx1, A2fhkh, 7asf

Between n-m times a single specific character

Pattern: z{3–6}

Matching Strings: a1112, bbbb3h,z5nhggggggkhg

Non-matching Strings: abc112, aaaaaaaabx3

Between n-m times any single character

Pattern: .{2–3}

Matching Strings: aa12, b333h, zbb123, aaac

Non-matching Strings: aaaa, a123, aaaac

One or more times each with multiple char wildcard

Pattern: aa+b*c+

Matching Strings: aabcc,aaabxc

Non-matching Strings: abc, aaacb,aaabc

[abc]+ (one or more of any a, b, or c character), .* (zero or more of any character).

Metacharacter (?-Single char 0 or 1 times)

Pattern: ab?c

Matching Strings: “abc” , “ac”

Non-matching Strings: abbc, aaacb,bc

Any Digit

Pattern: \d

Matching Strings: 1,34,6711

Non-matching Strings: a23,a,3a

Any Space/ tab

Pattern: a \s+ b

Matching Strings: “a b” , “a b” Between a and b there should be 1 or more spaces

Non-matching Strings: ab

Starts with (^) & Endswith($)

Pattern: ^Hi

Matching Strings: Hi, Hi there!

Non-matching Strings: Hellohi, Ana Hi

Pattern: Bye$

Matching Strings: Ana Bye, Bye, Good Bye

Non-matching Strings: Bye Ana

Group matching

To only allow certain type of files (here pdf) with certain prefix (here : file)

Pattern: ^(file.+).pdf$

Matching Strings: file1.pdf , filefile3.pdf

Non-matching Strings: 1.pdf, file.pdf.ppt

For amazing tutorials to interactively learn “Regex” visit

As a Tech Enthusiast, I love to exfoliate tech marvels each day passing by!