Java Regular Expressions

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Regular expression in Java is a sequence of characters defining search patterns in text. Ranging from single characters to complex patterns, they enable versatile text search and replace operations.

The java.util.regex package manages regular expressions in Java.

It includes the Pattern class for pattern definition, Matcher class for pattern searching, and PatternSyntaxException class for syntax errors in regex patterns.

Through these classes, Java offers robust regex capabilities for text processing tasks. Users can define patterns using Pattern, search for occurrences of these patterns using Matcher, and handle syntax errors with PatternSyntaxException.

This comprehensive package empowers developers to perform intricate text manipulations, enhancing Java's versatility in handling text-based tasks effectively. With regex, Java applications gain the ability to efficiently parse, search, and manipulate textual data, contributing to their functionality and flexibility in various domains.

Matcher Class

The Matcher class checks the Pattern against an input string.

The Matcher class also provides the matches(), replaceFirst(), and replaceAll() methods.

Apart from the find() method, there are also some other useful methods in the Matcher Class, some of which are implemented from the MatchResult interface:

Return type and Method nameDescription
int start()Returns the start index of the previous match (from MatchResult interface)
int end()Returns the offset after which the last character was matched (from MatchResult interface)
String group()Returns the input subsequence which was last matched in the previous match operation (from MatchResult interface)
int groupCount()Returns the number of groups that were captured in this Matcher’s pattern (from MatchResult interface)
String replaceFirst(String replacement) String replaceAll(String replacement)Replaces the first subsequence or all subsequences that matches the pattern, with the given replacement String
Matcher reset(CharSequence newInput)Resets the Matcher with the new input sequence

Pattern Class

The Pattern class compiles regex into a Pattern object.

The Pattern class also provides the split() method to split strings based on a regex, the pattern() method to return the regex string, and the flags() method to return the match flags.

Some more useful methods of the Pattern Class are:

Return type and Method nameDescription
Matcher matcher(CharSequence input)Creates a Matcher object that can be used to match the input parameter against our Pattern object. This is illustrated in detail in the next section when we discuss the Matcher Class.
int flags()Returns the pattern’s matched flags
static String quote(String s)Returns a literal pattern String for the given String s
Stream splitAsStream(CharSequence input)Returns a Stream of type String, from given input around matches of this pattern

Example of Regular Expression in Java

In this example, we demonstrate three ways to check if a string consists of exactly three digits using regular expressions. The Pattern class is used to compile the regex pattern, and the Matcher class is employed to perform matching operations. Finally, we print the results of the matches.

Regex Character Classes

SymbolDescriptionExample
XYRaw form charactersabc means a, b, then c
.Any single character.@abc means any character followed by @abc
[ ]Character group inside brackets[abc] means a or b or c
Character range[a-z0-9] means any character from a to z or 0 to 9
^Expression beginning^abc means the String starts with abc. [^abc] means any character except a or b or c
$Expression endcom$ means the String ends with com

Example

Regex Quantifiers

Quantifiers indicate quantities in our regular expression.

SymbolDescription
?Zero or one occurrence
*Zero or more occurrences
+One or more occurrences
{n}Exactly n occurrences
{n,}n or more occurrences
{m,n}At least m occurrences, but no more than n

Example

Regex Metacharacters

Meta characters use an escape character for specialized symbols.

SymbolDescription
\sWhitespace character
\SNon-whitespace character
\dAny digit
\DAny non-digit
\wAny word
\WAny non-word
\bWord boundary
\BA non-word boundary

Example

Conclusion

  • The regular expression in Java offers a versatile and powerful way to work with strings, allowing for complex pattern matching, searching, and text manipulation tasks that are essential in many programming scenarios.
  • The java.util.regex package provides a robust API, including the Pattern class for compiling regex patterns, the Matcher class for performing match operations, and the PatternSyntaxException class for handling syntax errors, which together offer comprehensive support for regex operations.
  • Regular expression in Java allows for precise pattern matching, including the use of character classes to define a set of characters, quantifiers to specify the number of occurrences, and metacharacters for special sequences, enhancing the efficiency and flexibility of text processing.
  • Given their complexity, regular expressions in Java can impact performance. Optimal use involves careful pattern crafting to balance functionality with efficiency, ensuring patterns are as specific as possible to minimize processing overhead.