Regular Expressions in JAVA

The regular expressions or regex as they are known  popularly are used for string handling purpose.

If we want to compare the two strings, if we want to replace some of the characters in the string, if we want to extract specific pattern in a particular string, then we go for regex.

This regex enables us to get the required information in very easy way.

Regex is always applied to the strings only. The simples regex is a dot(.) which matches with “a” or “z” or “1”.


The standard set of rules or matching symbols can be expressed in the form of following tables

Table No. 1


. Matches any character
^refex Matches regex that must match at the

beginning of the line

abc$ Matches regex that resides at the end of

the line

[abc] Matches either a or b or c
[abc][yz] Matches a or b or c followed by either y or


[^abc] Matches all the string that do not start with

either a or b or c

[a-z0-9] This depicts the range(-) in regex. It matches

any character from a to z followed by any integer

from 0 to 9

a|b Matches either a or b
ab Matches a followed by b only
$ Matches the end of the line i.e. blank line


Table No. 2

\d Matches any digit, short for [0-9]
\D Matches any non-digit, short for [^0-9]
\s Matches any whitespace character, short for

[ \t\n\r\f]

\S Matches any non-whitespace character,

short for [^\s]

\w Matches any word character, short for [a-


\W Matches any non-word character, short for


\S+ Matches several non-whitespace character
\b Matches a word boundary


Table No. 3


* Occurences of zero or more times, short for


+ Occurences of one or more times, short for


? Occurences of none or one times, short for


{x} Occurs x number of times
{x,y} Occurs between x and y times


In the upcoming blogs on regex, grouping concept will be discussed.
Hope you had a great read. Thank you.

Published by milindjagre

I founded my blog four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at for further information. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: