Regular Expressions In Java

Regular Expressions are a key element in programming. There are times that you can save a lot of time with a simple Regex. As a lot of my teachers said while I was studying, it is an important thing that you should be familiar with. Regular Expressions are designed to help you define a pattern to search for in a String. You can use it to check whether a parameter is valid or to find a specific pattern of text in a large file. In Java, you’ll be using java.util.regex package to implement Regex logics to your project.

Using built-in Regular Expressions Class in Java

In order to implement Regex in Java, you will need to get familiar with two basic class.

  1. Pattern Class (Javadoc)
  2. Matcher Class (Javadoc)

While Pattern Class is designed to compile your patterns from String, Matcher Class comes to your aid when you need to do some matching with your compiled pattern. Let’s look at it in a simple example.

//First Option
Pattern pattern = Pattern.compile("[^abc].*");
Matcher m1 = pattern.matcher("as");
Matcher m2 = pattern.matcher("fd");
Matcher m3 = pattern.matcher("yc");
boolean b = m1.matches();
boolean b2 = m2.matches();
boolean b3 = m3.matches();
//Second Option
boolean b4 = Pattern.compile("[^abc].*").matcher("as").matches();
boolean b5 = Pattern.compile("[^abc].*").matcher("fd").matches();
boolean b6 = Pattern.compile("[^abc].*").matcher("yc").matches();
//Third Option
boolean b7 = Pattern.matches("[^abc].*", "as");
boolean b8 = Pattern.matches("[^abc].*", "fd");
boolean b9 = Pattern.matches("[^abc].*", "yc");
System.out.println(b + "\n" + b2 + "\n" + b3 + "\n" + b4
        + "\n" + b5 + "\n" + b6 + "\n" + b7 + "\n"
        + b8 + "\n" + b9);
false
true
true
false
true
true
false
true
true

So as you can see above, there are multiple ways to do this. But the preferred method is generally the first one. As you can see in the second and third options Pattern.compile is processed in every line independently. You just compile the pattern only to discard after your first usage. This is more expensive than compiling once and running Matcher.matches on the same already compiled pattern over and over again. This is why the first option is almost always favorable. So if you will be checking Regex in a loop, compile the pattern outside the loop body then call Matcher.matches inside of it.

A few words on matches method

Using Regular expressions in Java sometimes might be confusing because can you miss a lot of little things like escape characters and whatnot. I want to share a piece of information that caused me to waste time when I was first using Regular Expressions in Java.

As you can see I did use the method matches to get results for my regex operation. This method might be confusing for people who are coming from different backgrounds since it only returns true if and only given string is fully matched with the pattern. So if you have a pattern like ([A-Z])+ Matcher.matches method will return true for “vegan” but return false for ” vegan” (with preceding whitespace) even you have a matching String in it.

Master it

Learn the expression elements, how they work, how they interact with one another. After a point you’ll notice sometimes It’s a lot faster to find something by writing a regex query in notepad++ or whatever text editor you use. It can even help you find some line in log files a whole lot easier than searching for an exact match. I would like to share a couple of resources for you to dive in. Go check them out.

https://regexr.com/

https://regex101.com/

Leave a Reply