Java Regex
Regex is the short form of regular expressions which are powerful tools for pattern matching and manipulation of strings. In Java, the java.util.regex package provides classes for working with regular expressions. We will learn the basics of Java regex and how you can leverage them in your applications in this article.
Creating a Regex Pattern
We will begin by constructing a Pattern object to utilize regular expressions in Java. The Pattern class is part of the java.util.regex package and is utilized to represent a compiled form of a regular expression. Here is a simple example:
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// Define a regex pattern
String regex = “ab+c”;
// Compile the regex into a Pattern
Pattern pattern = Pattern.compile(regex);
// Now, you can use the pattern for matching
String input = “abbc”;
Matcher matcher = pattern.matcher(input);
// Check for a match
if (matcher.matches()) {
System.out.println(“Input matches the pattern!”);
} else {
System.out.println(“No match found.”);
}
}
}
In this example, the regex pattern is “ab+c”, which matches strings like “abc”, “abbc”, “abbbc”, and so on.
Matcher Class
Get Arshika Singh’s stories in your inbox
Join Medium for free to get updates from this writer.
The Matcher class is utilized to perform match operations on a character sequence. It is constructed by calling the matcher() method on a Pattern object. Here is an example:
import java.util.regex.*;
public class MatcherExample {
public static void main(String[] args) {
String regex = “\\d+”; // Matches one or more digits
String input = “12345”;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// Check if the input contains a match
if (matcher.find()) {
System.out.println(“Match found: “ + matcher.group());
} else {
System.out.println(“No match found.”);
}
}
}
In this example, the regex pattern “\d+” matches one or more digits. The find() method is utilized to find the next subsequence of the input sequence that matches the pattern.
Common Regex Patterns
\d: Matches a digit (0–9).
\w: Matches a word character (alphanumeric + underscore).
\s: Matches a whitespace character.
.: Matches any character except a new line.
^: Anchors the regex at the start of the string.
$: Anchors the regex at the end of the string.
Quantifiers
Quantifiers specify the number of occurrences of a character or group in a pattern.
*: Matches 0 or more occurrences.
+: Matches 1 or more occurrences.
?: Matches 0 or 1 occurrence.
{n}: Matches exactly n occurrences.
{n,}: Matches n or more occurrences.
{n,m}: Matches between n and m occurrences.
Conclusion:
It is a valuable skill for Java developers to understand Java Regex. Regular expressions provide a concise and powerful toolset such as validating user input, searching for patterns in text or performing complex string manipulations. The java.util.regex package in Java empowers developers to harness the full potential of regular expressions in their applications.





