Java: Built-in String Regular Expression (RegEx) Methods

Java: Built-in String Regular Expression (RegEx) Methods

Regular Expressions (RegEx) are a powerful tool and help us match patterns in a flexible, dynamic and efficient way, as well as to perform operations based on the results.

In this short guide, we'll take a look at the built-in RegEx methods, which are a part of the String class and allow us to avoid the hassle of working with the Pattern and Matcher classes.

If you'd like to read more about Regular Expressions and the regex package, read out Guide to Regular Expressions in Java!

String RegEx Methods in Java

The regex package in the standard Java API has introduced us to the Pattern and Matcher classes that we can use to represent Regular Expressions and check for matches. Though, this requires the creation and use of two additional objects - which, although works just fine, is a bit verbose and unnecessary.

A cleaner, more elegant solution on the client's end was much needed, for simple matching, and the String class was imbued with a few methods pertaining to Regular Expressions.

What happens under the hood?

Well, exactly as you might've imagined - the methods ultimately call the classes from the regex module, and using String RegEx methods is technically the exact same as using the classes themselves - just cleaner and less verbose.

Note: For most cases, you'll prefer using the built-in methods, because of this.

If you'd like to read more about Regular Expressions and the regex package, read out Guide to Regular Expressions in Java!

The matches() Method

The matches() method works in much the same way as the matches() method of the Matcher object, returned from a Pattern object, given a certain RegEx. This is because it inherently calls these exact methods.

It accepts a string-represented Regular Expression and returns a boolean based on whether the entirety of the string matches the RegEx - beware that the entire string has to match it, otherwise, false is returned:

String string = "Hello there!";
boolean matches = string.matches(".*Hello.*");

if (matches) {
    System.out.println("General Kenobi.");
}

Note: The matches() method, surprisingly enough, doesn't support the addition of Pattern enums, such as CASE_INSENSITIVE and is actually CASE_SENSITIVE by default. For these, you'll have to use the source class itself.

Our RegEx checks for the sequence "Hello" with any number of characters before and after it, so naturally, it does match and the result is:

General Kenobi.

For those interested, the matches() method looks like this:

public boolean matches(String regex) {
    return Pattern.matches(regex, this);
}

Which just calls:

public static boolean matches(String regex, CharSequence input) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    return m.matches();
}

That being said, the method is case-sensitive by default.

The split() Method

The split() method is a commonly used method. Many are acquainted with the method being told that it splits the string based on the given character/delimiter, however, this isn't fully accurate.

The split() method splits the given string, on every occurrence of the given Regular Expression.

If your RegEx is a single character, it'll split on instances of that character - though, you're not limited to single characters. You can split the string on any RegEx:

The most common use-case is splitting an input String, in CSV format:

String countries = "England,Japan,Italy,Kenya,Mexico";
String[] splits = countries.split(",");

for (String country: splits){
    System.out.println(country);
}

This results in:

England
Japan
Italy
Kenya
Mexico

Additionally, sentences are oftentimes broken down into words by splitting on each " " (whitespace)`. Because of this, the common understanding of the method is that it splits on a certain character - but there's the possibility of getting creative here.

The replaceFirst() and replaceAll() Methods

The Matcher class doesn't only match - it can be used to replace certain parts of Strings, found via Regular Expressions.

To that end, you can use the short-hand replaceFirst() and replaceAll() methods of the String class, which calls the Matcher (which in turn calls String methods...) to modify a String, on the first (or all) occurrence of a given sequence is matched.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Both methods accept a RegEx and a replacement String - the replaceFirst() replaces the first occurrence of that sequence of characters with the replacement String, while the replaceAll() method replaces all occurrences:

String string = "Python is a general-purpose programming language. With Python, you can opt to create...";

string = string.replaceAll("Python", "Jaffa");
System.out.println(string);

string = string.replaceFirst("Jaffa", "Java");
System.out.println(string);

Both methods return a new String object, so make sure to assign it to a new reference variable, or the same one you've already got lying around. In the first println() call, we'll have both "Python" sequences turned into "Jaffa", and in the second println() call, we'll have turned the first "Jaffa" into "Java":

Jaffa is a general-purpose programming language. With Jaffa, you can opt to create...
Java is a general-purpose programming language. With Jaffa, you can opt to create...

Conclusion

In this short guide, we've taken a look at the built-in RegEx methods of the String class in Java.

To deal with Regular Expressions, we can use the Pattern and Matcher classes of the regex package - though, for many day-to-day use-cases with Strings, you can avoid the boilerplate by using the built-in methods.

These methods ultimately use the regex package, so the same level of performance and results are to be expected.

Last Updated: October 21st, 2021
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:
     
     
     

    Better understand your data with visualizations

    With over 330+ pages, you'll learn the ins and outs of visualizing data in Python with popular libraries like Matplotlib, Seaborn, Bokeh, and more.

    © 2013-2021 Stack Abuse. All rights reserved.