Exploring Regex in Java.

Regex is common in all language so discussion here gives you a idea about regex.

What is Regex

Regex are Regular Expressions , very handy and powerful tool. Used by developers most of the time.It is common in all languages out there.

Regex is all about pattern matching.

Let me give you an example “There are thousands of articles on Medium and all have text format , now you were lazy and don’t want to read the full article you just want to read some specific things in the article here you can use REGEX the specific things was your pattern and target text was your article”

Some very useful and helpful tools

There are tons of visual regex tester but we will be using this . Visual Regex tester shows you how your result will look like.Lets take a look

So this is your tester there are two important fields — target text and other is your Regular Expression(i.e. your pattern). Go and play with it very simple.

`A example of visual regex.
In the above example i wanted to get the numeric character from the text so i used [\d] the regex for numeric character .

How to write Regular Expressions

It needs lot of practice to get familiar to all the regular expression , while you don’t need to memorize most of the time all refer to the nice short documentation here . Once you will get used to it feels so good.So let’s get started writing few.

Let’s start with an example “We are making a postal code validator — which will validate all postal codes which are of India”.

Lets first visualize it in tester

This was the result :)

Hold on and we will get to it.First of all , the postal codes of India are of 6 digits so we have to accept only 6 digits and the character should be number.

Now take a look at my regex \d{6} here \d means I am talking about numbers ,now I have added a quantifier {} and have specified it should only accept 6 digits.

How to code it up?

No talk now!!!

import java.io.Console;
class Reggie {
public static void main(String[] args) {
Console console = System.console();
String getCode = console.readLine("Enter your postal code \n");
if(getCode.matches("\\d{6}")) {
System.out.printf("Zip code %s belongs to India " , getCode);
}
else
System.out.printf("Bam it is not validated!!!");
}
}

So let understand what I did above.

I used Console class to get my input.
Accepted the zip-code which is to be validated in an string format.
Strings have that matches function which I used and passed my regex there as a string.
Now take a look at my regex which I have passed as a String in matcher().I have used here \\d because \d means escape sequence in Java so using two backslash like \\d it tells that I am talking about actual \d.

I know you will not get everything at once but it’s fine. Even I was not , it comes by using it the more you use eventually you will get better just don’t leave it until you r comfortable with it.

Lets take a look at one more example , “ Suppose we have to parse a cover letter of job aplicant and we are looking for a guy who is comfortable in HTML , CSS and Javascript”

First taking a look at visual tester.

Here the OR(|) operator is introduced which means one or more. You can see in the regex something like (?i) which means ignore the case in any case it will match and \b here means the word boundary .

Before heading towards the code I will recommend you to see this documentation.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternRegex {
public static void main(String[] args) {
String getLetter = "I am a full stack developer.I am fluent in HTML , CSS and Javascript also Javascripter";
Pattern pattern = Pattern.compile("(?i)HTML\\b|CSS\\b|Javascript\\b");
Matcher matcher = pattern.matcher(getLetter);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}

What the hell is this???

Keep calm and everything will be crystal clear.

Java has its own class for Regex , I have used two subclasses Pattern and Matcher which i’ll be explaining.

Pattern is a class which takes your pattern which you r looking for in the target text.Pattern has static method compile which is the place where you have to pass your regex as I have did.
Matcher is nice little helper class which keeps track of what was matched.It has a find() method which keep track of what was matched and also shifts the internal state to next word which is to be matched.I have also used group method it groups up your matched text to produce result.

The output we will get from the code is

HTML
CSS
Javascript

Look at the last word of the String which I passed it was not matched only because of \b meaning boundary of the word.

So review of what we learnt

  1. How to use Visual Regex Tester.
  2. No need to learn the regex expression we have documentation for that.
  3. How to read regular expressions.
  4. How to write code for it.
  5. Pattern and Matcher class and some cool methods provided by these classes.

It is absolutely okay that everything will not be clear , but as you will use it 2 or 3 times you will understand.

Hope you like this article.Please hit the heart button , share if u feel it is helpful and comment some cool things we can do using Regex :)

Show your support

Clapping shows how much you appreciated PRANJAL AGNIHOTRI’s story.