The Java Regex, also known as Regular Expression, serves as an interface for specifying a template to search for or modify strings.
Java regex tutorial is commonly utilized for specifying constraints on strings like password and email validation. Upon completion of this tutorial, you will have the ability to evaluate your regular expressions using the Java Regex Tester Tool.
The Java Regex API consists of one interface and three classes located within the java.util.regex package.
java.util.regex package
The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.
- MatchResult interface
- Matcher class
- Pattern class
- PatternSyntaxException class
Matcher class
The class implements the MatchResult interface, serving as a regular expression engine utilized for executing match operations on a sequence of characters.
| No. | Method | Description |
|---|---|---|
1 |
boolean matches() | test whether the regular expression matches the pattern. |
2 |
boolean find() | finds the next expression that matches the pattern. |
3 |
boolean find(int start) | finds the next expression that matches the pattern from the given start number. |
4 |
String group() | returns the matched subsequence. |
5 |
int start() | returns the starting index of the matched subsequence. |
6 |
int end() | returns the ending index of the matched subsequence. |
7 |
int groupCount() | returns the total number of the matched subsequence. |
Pattern class
It represents the compiled form of a regular expression, serving to specify a pattern for the regex engine.
| No. | Method | Description |
|---|---|---|
1 |
static Pattern compile(String regex) | compiles the given regex and returns the instance of the Pattern. |
2 |
Matcher matcher(CharSequence input) | creates a matcher that matches the given input with the pattern. |
3 |
static boolean matches(String regex, CharSequence input) | It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern. |
4 |
String[] split(CharSequence input) | splits the given input string around matches of given pattern. |
5 |
String pattern() | returns the regex pattern. |
Example of Java Regular Expressions
Three different methods can be used to create the regular expression example in the Java programming language.
Example
//Creating an example to understand the use of Java Regular Expression
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();
//3rd way
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
}}
Output
true true true
Regular Expression . Example
The . (dot) represents a single character.
Example
//Java Program to understand the use of dot . quantifier
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
System.out.println(Pattern.matches(".s", "as"));//true (2nd char is s)
System.out.println(Pattern.matches(".s", "mk"));//false (2nd char is not s)
System.out.println(Pattern.matches(".s", "mst"));//false (has more than 2 char)
System.out.println(Pattern.matches(".s", "amms"));//false (has more than 2 char)
System.out.println(Pattern.matches("..s", "mas"));//true (3rd char is s)
}}
Java Regex Character classes
| No. | Character Class | Description | ||
|---|---|---|---|---|
1 |
[abc] | a, b, or c (simple class) | ||
2 |
[^abc] | Any character except a, b, or c (negation) | ||
3 |
[a-zA-Z] | a through z or A through Z, inclusive (range) | ||
4 |
[a-d[m-p]] | a through d, or m through p: [a-dm-p] (union) | ||
5 |
[a-z&&[def]] | d, e, or f (intersection) | ||
6 |
[a-z&&[^bc]] | a through z, except for b and c: [ad-z] (subtraction) | ||
7 |
7 | <span> |
Java Regular Expression: Character classes Example
Example
//Java Program to understand the use of Regex Character class
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
System.out.println(Pattern.matches("[amn]", "abcd"));//false (not a or m or n)
System.out.println(Pattern.matches("[amn]", "a"));//true (among a or m or n)
System.out.println(Pattern.matches("[amn]", "ammmna"));//false (m and a comes more than once)
}}
Java Regex Quantifiers
Quantifiers determine how many times a character can appear.
| Regex | Description |
|---|---|
X? |
X occurs once or not at all |
X+ |
X occurs once or more times |
X* |
X occurs zero or more times |
| X{n} | X occurs n times only |
| X{n,} | X occurs n or more times |
| X{y,z} | X occurs at least y times but less than z times |
Java Regular Expression: Character classes and Quantifiers Example
Example
//Java Program to understand the use of regex quantifiers
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
System.out.println("? quantifier ....");
System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time)
System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (a m and n comes more than one time)
System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a comes more than one time)
System.out.println(Pattern.matches("[amn]?", "am"));//false (a or m or n must come one time)
System.out.println("+ quantifier ....");
System.out.println(Pattern.matches("[amn]+", "a"));//true (a or m or n once or more times)
System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a comes more than one time)
System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a or m or n comes more than once)
System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z and t are not matching pattern)
System.out.println("* quantifier ....");
System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (a or m or n may come zero or more times)
}}
Java Regex Metacharacters
Metacharacters in regular expressions function as shorthand codes.
| Regex | Description |
|---|---|
. |
Any character (may or may not match terminator) |
d |
Any digits, short of [0-9] |
D |
Any non-digit, short for [^0-9] |
s |
Any whitespace character, short for [\t\n\x0B\f\r] |
S |
Any non-whitespace character, short for [^\s] |
w |
Any word character, short for [a-zA-Z_0-9] |
W |
Any non-word character, short for [^\w] |
b |
A word boundary |
B |
A non word boundary |
Java Regular Expression: Metacharacters Example
Example
//Java Program to understand the use of Regex Metacharacters
import java.util.regex.Pattern;
public class Main{
public static void main(String args[]){
System.out.println("metacharacters d....");//d means digit
System.out.println(Pattern.matches("\\d", "abc"));//false (non-digit)
System.out.println(Pattern.matches("\\d", "1"));//true (digit and comes once)
System.out.println(Pattern.matches("\\d", "4443"));//false (digit but comes more than once)
System.out.println(Pattern.matches("\\d", "323abc"));//false (digit and char)
System.out.println("metacharacters D....");//D means non-digit
System.out.println(Pattern.matches("\\D", "abc"));//false (non-digit but comes more than once)
System.out.println(Pattern.matches("\\D", "1"));//false (digit)
System.out.println(Pattern.matches("\\D", "4443"));//false (digit)
System.out.println(Pattern.matches("\\D", "323abc"));//false (digit and char)
System.out.println(Pattern.matches("\\D", "m"));//true (non-digit and comes once)
System.out.println("metacharacters D with quantifier....");
System.out.println(Pattern.matches("\\D*", "mak"));//true (non-digit and may come 0 or more times)
}}
Java Regular Expression Question 1
Generate a regular expression pattern that allows for the inclusion of alphanumeric characters exclusively, with a fixed length of six characters.
import java.util.regex.*;
class RegexExample6{
public static void main(String args[]){
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));//true
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));//false (more than 6 char)
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));//true
System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));//false ($ is not matched)
}}
Regular Expression Question 2
/*Create a regular expression that accepts 10 digit numeric characters
starting with 7, 8 or 9 only.*/
import java.util.regex.*;
class RegexExample7{
public static void main(String args[]){
System.out.println("by character classes and quantifiers ...");
System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));//true
System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));//false (11 characters)
System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));//false (starts from 6)
System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));//true
System.out.println("by metacharacters ...");
System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));//true
System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));//false (starts from 3)
}}
Java Regex Finder Example
import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
public class RegexExample8{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("Enter regex pattern:");
Pattern pattern = Pattern.compile(sc.nextLine());
System.out.println("Enter text:");
Matcher matcher = pattern.matcher(sc.nextLine());
boolean found = false;
while (matcher.find()) {
System.out.println("I found the text "+matcher.group()+" starting at index "+
matcher.start()+" and ending at index "+matcher.end());
found = true;
}
if(!found){
System.out.println("No match found.");
}
}
}
}
Output:
Enter regex pattern: java
Enter text: this is java, do you know java
I found the text java starting at index 8 and ending at index 12
I found the text java starting at index 26 and ending at index 30
Let's delve into a comprehensive explanation of Java Regular Expressions with the assistance of a Java sample program.
Filename: JavaRegrex.java
// Java program for demonstrating the features and functionalities of Java Regrex
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JavaRegrex {
public static void main(String[] args) {
// Example 1: Matching a specific pattern
System.out.println("Example 1: Matching a specific pattern");
// Define the text in which to search
String text1 = "The quick brown fox jumps over the lazy dog";
// Define the pattern to match
String pattern1 = "fox";
// Check if the pattern matches the text using Pattern.matches()
boolean matches1 = Pattern.matches(pattern1, text1);
// Output whether the text contains the pattern
System.out.println("Does the text contain the word 'fox'? " + matches1);
// Example 2: Using Pattern and Matcher classes
System.out.println("\nExample 2: Using Pattern and Matcher classes");
// Define the text to search within
String text2 = "Java is a programming language";
// Define the regex pattern to match 3-letter words
String regex2 = "\\b\\w{3}\\b";
// Compile the regex pattern into a Pattern object
Pattern pattern2 = Pattern.compile(regex2);
// Create a Matcher object to apply the pattern on the text
Matcher matcher2 = pattern2.matcher(text2);
// Find and output all matches found by iterating over Matcher's results
System.out.print("3-letter words in the text: ");
while (matcher2.find()) {
System.out.print(matcher2.group() + " ");
}
System.out.println();
// Example 3: Replacing matched patterns
System.out.println("\nExample 3: Replacing matched patterns");
// Define the text in which to search
String text3 = "The cat sat on the mat";
// Define the regex pattern to match the word 'cat'
String regex3 = "\\bcat\\b";
// Define the replacement string
String replacement3 = "dog";
// Replace all occurrences of 'cat' with 'dog' using String's replaceAll()
// method
String replacedText3 = text3.replaceAll(regex3, replacement3);
// Output the original and replaced text
System.out.println("Original text: " + text3);
System.out.println("Replaced text: " + replacedText3);
// Example 4: Splitting text using regex
System.out.println("\nExample 4: Splitting text using regex");
// Define the text to split
String text4 = "apple,banana,orange,grape";
// Define the regex pattern for splitting by comma
String regex4 = ",";
// Split the text using the regex pattern into an array of strings
String[] parts4 = text4.split(regex4);
// Output the split parts
System.out.print("Fruits separated: ");
for (String part : parts4) {
System.out.print(part + " ");
}
System.out.println();
// Example 5: Validating email addresses
System.out.println("\nExample 5: Validating email addresses");
// Define an array of email addresses to validate
String[] emails = { "user@example.com", "invalid.email.com", "another.user@domain.co" };
// Define the regex pattern for validating email addresses
String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
// Compile the regex pattern into a Pattern object
Pattern emailPattern = Pattern.compile(emailRegex);
// Validate each email address by matching against the regex pattern
System.out.println("Validating email addresses:");
for (String email : emails) {
// Create a Matcher object for each email
Matcher emailMatcher = emailPattern.matcher(email);
// Output the result of validation
System.out.println(email + ": " + emailMatcher.matches());
}
// Example 6: Extracting domain names from URLs
System.out.println("\nExample 6: Extracting domain names from URLs");
// Define a URL
String url = "https://www.example.com";
// Define the regex pattern for extracting domain names from URLs
String domainRegex = "^(https?://)?(www\\.)?([a-zA-Z0-9-]+\\.[a-z]{2,})";
// Compile the regex pattern into a Pattern object
Pattern domainPattern = Pattern.compile(domainRegex);
// Create a Matcher object for the URL
Matcher domainMatcher = domainPattern.matcher(url);
// Find the domain name by matching against the regex pattern and output it
if (domainMatcher.find()) {
System.out.println("Domain extracted from URL: " + domainMatcher.group(3));
}
}
}
Output:
Example 1: Matching a specific pattern
Does the text contain the word 'fox'? false
Example 2: Using Pattern and Matcher classes
3-letter words in the text:
Example 3: Replacing matched patterns
Original text: The cat sat on the mat
Replaced text: The dog sat on the mat
Example 4: Splitting text using regex
Fruits separated: apple banana orange grape
Example 5: Validating email addresses
Validating email addresses:
user@example.com: true
invalid.email.com: false
another.user@domain.co: true
Example 6: Extracting domain names from URLs
Domain extracted from URL: example.com
Let's explore another instance to gain a deeper understanding of Java regular expressions.
Filename: RegexFeatures.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexFeatures {
public static void main(String[] args) {
// Example 1: Extracting phone numbers from text
System.out.println("Example 1: Extracting phone numbers from text");
// Define the text containing phone numbers
String text1 = "Please call 123-456-7890 for assistance or 987-654-3210 for inquiries.";
// Define the regex pattern to match phone numbers in the format xxx-xxx-xxxx
String phoneRegex1 = "\\b\\d{3}-\\d{3}-\\d{4}\\b";
// Compile the regex pattern into a Pattern object
Pattern phonePattern1 = Pattern.compile(phoneRegex1);
// Create a Matcher object for the text
Matcher phoneMatcher1 = phonePattern1.matcher(text1);
// Find and output all matched phone numbers
System.out.print("Phone numbers found in the text: ");
while (phoneMatcher1.find()) {
System.out.print(phoneMatcher1.group() + " ");
}
System.out.println();
// Example 2: Extracting URLs from text
System.out.println("\nExample 2: Extracting URLs from text");
// Define the text containing URLs
String text2 = "Visit our website at https://www.example.com or http://another.example.org";
// Define the regex pattern to match URLs
String urlRegex2 = "\\bhttps?://\\S+\\b";
// Compile the regex pattern into a Pattern object
Pattern urlPattern2 = Pattern.compile(urlRegex2);
// Create a Matcher object for the text
Matcher urlMatcher2 = urlPattern2.matcher(text2);
// Find and output all matched URLs
System.out.print("URLs found in the text: ");
while (urlMatcher2.find()) {
System.out.print(urlMatcher2.group() + " ");
}
System.out.println();
// Example 3: Matching email addresses in text
System.out.println("\nExample 3: Matching email addresses in text");
// Define the text containing email addresses
String text3 = "Contact us at email@example.com or another.email@example.org";
// Define the regex pattern to match email addresses
String emailRegex3 = "\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b";
// Compile the regex pattern into a Pattern object
Pattern emailPattern3 = Pattern.compile(emailRegex3);
// Create a Matcher object for the text
Matcher emailMatcher3 = emailPattern3.matcher(text3);
// Find and output all matched email addresses
System.out.print("Email addresses found in the text: ");
while (emailMatcher3.find()) {
System.out.print(emailMatcher3.group() + " ");
}
System.out.println();
// Example 4: Finding dates in text
System.out.println("\nExample 4: Finding dates in text");
// Define the text containing dates
String text4 = "Important meetings on 2024-02-15 and 2024/02/20";
// Define the regex pattern to match dates in the format yyyy-mm-dd or yyyy/mm/dd
String dateRegex4 = "\\b\\d{4}[-/]\\d{2}[-/]\\d{2}\\b";
// Compile the regex pattern into a Pattern object
Pattern datePattern4 = Pattern.compile(dateRegex4);
// Create a Matcher object for the text
Matcher dateMatcher4 = datePattern4.matcher(text4);
// Find and output all matched dates
System.out.print("Dates found in the text: ");
while (dateMatcher4.find()) {
System.out.print(dateMatcher4.group() + " ");
}
System.out.println();
}
}
Output:
Example 1: Extracting phone numbers from text
Phone numbers found in the text: 123-456-7890 987-654-3210
Example 2: Extracting URLs from text
URLs found in the text: https://www.example.com http://another.example.org
Example 3: Matching email addresses in text
Email addresses found in the text: email@example.com another.email@example.org
Example 4: Finding dates in text
Dates found in the text: 2024-02-15 2024/02/20
In Java, regular expressions offer a versatile and robust mechanism for handling strings, enabling the execution of intricate matching and manipulation tasks using straightforward patterns.
Java regex MCQ
- What does the regex pattern [a-zA-Z] match?
- Any single lowercase letter
- Any single uppercase letter
- Any single letter (lowercase or uppercase)
- Any single digit
Explanation: The pattern [a-zA-Z] matches any single character that is either a lowercase or uppercase letter.
- Which regex pattern matches a string that starts with "Java"?
- ^Java
- Java$
- Java.*
- .*Java
Explanation: The ^ character indicates the start of a string. Therefore, ^Java matches any string that starts with "Java".
- What is the purpose of the \d in Java regex?
- It matches any whitespace character.
- It matches any digit.
- It matches any non-digit character.
- It matches any alphabetical character.
Explanation: The metacharacter \d in regular expressions matches any single digit character, which is the same as specifying a range from 0 to 9 using [0-9].
- What is the representation for a space character in Java regex?
Explanation: The \s shorthand in regex matches any whitespace character, including spaces, tabs, and line breaks.
- Which regex pattern matches an email address?
- \w+@\w+\.\w+
- \d+@\d+\.\d+
- \s+@\s+\.\s+
- \W+@\W+\.\W+
The regular expression \w+@\w+\.\w+ is designed to match a basic email address format. Here, \w+ denotes one or more alphanumeric characters, while the symbols @ and . are matched literally.