×

Java Regular Expressions

Java Regular Expressions

The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as a single character or can be a pattern of characters to make a complex pattern. To work with regular expression, one has to import the package java.util.regex. The package provides the following classes and interfaces.

Matcher Class: The class implements the MatchResult interface. It is used for pattern searching

Pattern Class: It defines the pattern that has to be searched.

PatternSyntaxException Class: The class checks for the syntactical error in the regular expression pattern.

Matcher Class


The following table enlists the pre-defined methods of the Matcher class:

Method NameDescription
int start()Returns the first sequence of the matching sequence.
int groupCount()Returns the total count of sequences that are matched.
String group()Returns the sequence that is matched.
public boolean find()Searches for the next sequence that matches with the given pattern. If the sequence is found, returns false, else returns true. 
public boolean find(int st)Searches for the sequence that matches with the given pattern from the index st. If any match is found, returns false, else returns true. 
public boolean matches()The method tries to match the regular expression pattern with the input sequence. If any mismatch is found, returns false, else returns true. 

Pattern Class


The following table enlists the pre-defined methods of the Pattern class:

Method NameDescription
public Matcher matcher(CharSequence cs)Creates a sequence in which the defined pattern has to be found.
public static boolean matches(String re, CharSequence cs)A static method that searches the regular expression re in the sequence cs.
public String pattern()Returns the sequence that is matched.
public String[] split(CharSequence cs, int limit)An array of string is returned by this method by breaking the input on the basis of matches with the given pattern. The second parameter limit determines the number of times the split() method is called.
public static Pattern compile(String rgx)The method compiles the string rgx to generate a pattern. The pattern is then returned.

Let’s understand the concept of regular expression through a Java program.

Java Program

Consider the following program that shows how to use regular expression.

FileName: RegexExample.java

 // importing the class Matcher
import java.util.regex.Matcher;
// importing the class Pattern
import java.util.regex.Pattern;
public class RegexExample
{
// main method       
public static void main(String argvs[])
{
// the pattern is Tutorial & example 
Pattern pt = Pattern.compile("Tutorial & example", Pattern.CASE_INSENSITIVE);
// the input sequence in which the pattern is searched.      
Matcher matcherObj = pt.matcher("Visit tutorial & example for learning about Java!");
// invoking the find() method       
boolean isMatchFound = matcherObj.find();
// checking whether match is found or not      
if(isMatchFound)
{
  System.out.println("Match found for the given pattern.");
}
else
{
 System.out.println("Match is not found for the given pattern.");
}
}
} 

Output:

Match found for the given pattern.


Explanation: The second parameter (Pattern.CASE_INSENSITIVE) in the compile() method is a flag that indicates that while making the pattern searching, the case sensitivity should not be taken into consideration. By default, the compile() method assumes that case sensitivity is present. The second parameter is optional and can be omitted. The matcher() method returns the object of the Matcher class. On this returned object, the find() method is invoked to check whether the regular expression pattern is available in the sequence or not.

Metacharacters

The characters that have special meaning are known as metacharacters. The following table shows the commonly used metacharacters in a regular expression.

MetacharactersDefinition
|Checks for any one of the patterns separated by |. For example, fish|dog|cat
\dLooks for a digit
\sLooks for a whitespace character
\uxxxxLooks for a Unicode character with the help of hexadecimal number xxxx
^Looks for a match in the starting of the string, e.g., ^World
$Looks for a match in the ending of the string, e.g., World$
.Looks for any single instance of character
\bLooks for a match either at the starting or at the ending of the string, e.g., \bWorld or World\b
\wLooks for any word character

Let’s use the metacharacters in a Java program.

FileName: RegexMetacharactersExample.java

 // import statements
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMetacharactersExample
{
// main method
public static void main( String argvs[])
{
// regular expression using \b
String rx = "own\\b";
String input = "crown own town brown owner grown flown blown";
Pattern ptrn = Pattern.compile(rx);
Matcher matcher = ptrn.matcher(input);
int cnt = 0;
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for the word \"own\" is : " + cnt);
// regular expression using |
rx = "world | hello";
input = "world is hello is world is hello is";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input); 
cnt = 0; // resetting the value of the cnt is 0
// checks for either world or hello
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for the word \"world\" and \"hello\" is : " + cnt);
// regular expression using ^ 
rx = "^hello";
input = "hello hello is world is hello is";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input);
cnt = 0; // resetting the value of the cnt is 0
// checks whether the input string starts with the word hello or not
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for the word \"hello\" is : " + cnt);
// regular expression using $
rx = "hello$";
input = "hello is world is hello";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input);
cnt = 0; // resetting the value of the cnt is 0
// checks whether the input string ends with the word hello or not
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for the word \"hello\" is : " + cnt);
// regular expression using .
rx = ".";
input = "hello world";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input);
cnt = 0; // resetting the value of the cnt is 0
// checks whether the input string ends with the word hello or not
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Total number of characters are : " + cnt);
// regular expression using \d
rx = "hello\\d";
input = "hello hello hello9";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input);
cnt = 0; // resetting the value of the cnt is 0
// checks whether the string contains hello[0-9]
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for hello[0-9] : " + cnt);
// regular expression using \s
rx = "hello\\s";
input = "hello hello hello9";
ptrn = Pattern.compile(rx);
matcher = ptrn.matcher(input);
cnt = 0; // resetting the value of the cnt is 0
// checks whether the string contains hello with a whitespace
while(matcher.find())
{
 cnt  = cnt + 1;
}
System.out.println("Number of matches for hello with whitespace: " + cnt); 
}
} 

Output:

 Number of matches for the word "own" is : 7
Number of matches for the word "world" and "hello" is : 4
Number of matches for the word "hello" is : 1
Number of matches for the word "hello" is : 1
Total number of characters are : 11
Number of matches for hello[0-9] : 1
Number of matches for hello with whitespace: 2 

Quantifiers

Quantifiers determine the number of characters or groups that should be present in the input to get a match.

QuantifiersDescription
Y*Looks for a string that contains 0 or greater than 0 occurrences of Y.
Y+Looks for a string that contains at least one occurrence of Y
Y{n, }Looks for a string that contains at least n occurrences of Y
Y{n}Looks for a string that contains exactly n occurrences of Y
Y(n1, n2}Looks for a string that contains at least n1 occurrences of Y but does not contain greater than n2 occurrences of Y
Y?Looks for 0 or 1 occurrences of Y

Java Program


The following program uses the quantifiers defined above.

FileName: QuantifiersExample.java

 // import statements
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuantifiersExample
{
// main method
public static void main(String argvs[])
{
System.out.println("For + quantifiers \n");
// regular expression for at least one 't'.
String rx = "t+";
Pattern ptrn = Pattern.compile(rx);
// Creating an object of the Matcher class
Matcher mtchr = ptrn.matcher("ttttst");
while (mtchr.find())
{
System.out.println("Pattern found from " + mtchr.start() + " to " + (mtchr.end() - 1));
}
System.out.println("\n");
// s t or k can appear zero or one time
rx = "[stk]?";
System.out.println("For ? quantifier \n");
ptrn = Pattern.compile(rx);
mtchr = ptrn.matcher("ssttkk");
while (mtchr.find())
{
System.out.println("Pattern found at index " + mtchr.start());
}
// s t or k can appear zero or more times
rx = "[stk]*";
System.out.println();
System.out.println("For * quantifier \n");
ptrn = Pattern.compile(rx);
mtchr = ptrn.matcher("ssttkk");
while (mtchr.find())
{
System.out.println("Pattern found at index " + mtchr.start());
}
// k has to appear at least 3 times
rx = "k{3,}";
System.out.println();
System.out.println("For {n, } quantifier \n");
ptrn = Pattern.compile(rx);
mtchr = ptrn.matcher("ssttkkkk");
while (mtchr.find())
{
System.out.println("Pattern found at index " + mtchr.start());
}
// k has to appear at least 3 times but not greater than 6 times
rx = "k{3,6}";
System.out.println();
System.out.println("For {n, m} quantifier \n");
ptrn = Pattern.compile(rx);
mtchr = ptrn.matcher("ssttkkkkkkkkkkkkkk");
while (mtchr.find())
{
System.out.println("Pattern found from " + mtchr.start() + " to " + (mtchr.end() - 1));
}
// k has to appear exactly 3 times
rx = "k{3}";
System.out.println();
System.out.println("For {n} quantifier \n");
ptrn = Pattern.compile(rx);
mtchr = ptrn.matcher("ssttkkkkkkkkkkkkkk");
while (mtchr.find())
{
System.out.println("Pattern found from " + mtchr.start() + " to " + (mtchr.end() - 1));
}
}
} 

Output:

 For + quantifiers
Pattern found from 0 to 3
Pattern found from 5 to 5
For ? quantifier
Pattern found at index 0
Pattern found at index 1
Pattern found at index 2
Pattern found at index 3
Pattern found at index 4
Pattern found at index 5
Pattern found at index 6
For * quantifier
Pattern found at index 0
Pattern found at index 6
For {n, } quantifier
Pattern found at index 4
For {n, m} quantifier
Pattern found from 4 to 9
Pattern found from 10 to 15
For {n} quantifier
Pattern found from 4 to 6
Pattern found from 7 to 9
Pattern found from 10 to 12
Pattern found from 13 to 15 

Explanation: The + quantifier takes all the matching characters at a time. Thus, following the greedy approach. Hence, all the indices of ‘t’ is taken from 0 to 3. Then, ‘s’ comes, which is not the part of the regular expression. After that a single ‘t’ occurs at index 5, which is represented in the output too.

The ? quantifier takes one character at a time. Therefore, in the output, we see every index from 0 to 5. The 6th index is shown because the ? quantifier also considers zero characters. After the 5th index, the input string finishes. Hence, the zero-character condition becomes true, and the 6th index is displayed in the output.

For the * quantifier also, the 6th index is shown because the * quantifiers also consider the zero-character condition. However, the * quantifier processes all the matching characters at a time. Therefore, we only see indices 0 and 6 in the output.

For the {n, } quantifier, the processing happens for greater than or equal to n matching characters at a time. Therefore, index 4 is seen in the output.

For the {n, m} quantifier (m should be greater than or equal to n), the processing happens for any matching characters whose frequency of occurrences lies between n and m at a time. If the frequency of occurrences happens to be more than m, then the frequency till m is considered, and in the next iteration, the remaining occurrences are considered. The same is evident by looking at the output.

For the {n} quantifier also, only the frequency of occurrences till n is considered. In the next iteration, the rest of the frequency of occurrences is considered. As n = 3 in our case, we see a gap of 3 in the output.


Related Topics

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

How to download and install Eclipse in Windows?

Download and Install Eclipse on Windows Eclipse is an open source IDE (Integrated Development Environment) which is used to help the programmers to provide a platform to write and run the...

1 minute read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.