Regular Expression part 2 in java

In the part 1 , we have seen what is regular expression and its syntax. In part 2, we will understand how to use regular expression with examples.

Example 1:

We can use any string as regular expression. Let’s consider a string “I like this book” is an output of a any program. Depending on some conditions , the string may vary like “I like this movie”, “I like this house” etc.. As you can observe, only words are changing like movie, house, book etc.

Now, our requirement is we need to create a regular expression which can fit for all string output. Let’s see what we can do?

As you can observe, we have used character class (\w) and quantifier (+) for making this regular expression. ‘\w’ means match word character and ‘+’ means match one or more characters.Now, the pattern will match any word instead of ‘book’.

Now, suppose you got the string “I like this – book”. In that case, regular expression will fail because it one character ‘-‘ which is not handled in regular expression pattern. To match this string , we have tweak our pattern a bit which will be like given below.

Now, it will match with the strings like “I like this – book”,I like this – movie” etc.

Example 2:

Now, suppose we know the output string in advance, so we will be aware of all the words like movie, book, house etc. In that case, we can group all these words together and put an OR condition. Let’s see how to do this.

We can use ‘()‘ to group and a meta-character ‘|’ (pipe symbol) to specify all words which can match with the regular expression.

This way we can do the grouping for multiple expressions.  Let’s few matched or unmatched string with the above expression.

Example 3:

In this example, we will have pattern defined for IP address and then we will validate it against multiple IP address. Regular expression returns true if matches else false. We can use this kind of validation for other input like email id, phone number or some web form data etc.

Let’s understand with an example.

Valid ip :
Valid ip :
Valid ip :

Example 4:

In this example, we have created a regular expression for a customized email id as er our requirement. Rules are not for actual email formatting but just to understand the usage of regular expression.

Rules are like part before ‘@’ can accept alphabets, numeric, underscore(_), plus sign(+) ,hyphen(-) and after ‘@’ it should have a pattern like,, etc.

Let’s check out the example.

Valid email address :
Valid email address :
Valid email address :
Valid email address :
Valid email address :
Valid email address :

Example 5:

We can extract some specific values out of the large text body as per the requirement. Let’s understand with the example.

In this example wee need to extract the words like book, movie, theater etc.

Item found : book
Item found : movie
Item found : theater

Example 6: 

We can replace or modify the required value in the text body as shown in the below example. We are changing the A’s EmpID.

Output: A has EmpID=3456.B has EmpID=6789.

At some point of time, every programmer use regular expression.The complexity of regular expression may vary but with practice it will become easy to handle.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Regular expression part 1 in java
Find common elements among two unsorted arrays
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

1 Response

  1. August 14, 2015

    […] We will see the rest of the regular expression in the part 2. […]

Leave a Reply

Your email address will not be published. Required fields are marked *