Java Regular Expressions (Regex)Last Updated : 12 Jan 2026 The Java Regex or Regular Expression is an API used to define patterns for searching or manipulating strings. It is widely used to define constraints on strings, such as password and email validation. After learning the Java Regex, you will be able to test your regular expressions using the Java Regex Tester tool. The Java Regex API provides one interface and three classes in the java.util.regex package java.util.regex packageThe Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides following classes and interfaces for regular expressions.
![]() Matcher classIt implements the MatchResult interface. It is a regex engine which is used to perform match operations on a character sequence.
Pattern classIt is the compiled version of a regular expression. It is used to define a pattern for the regex engine.
Example of Java Regular ExpressionsThere are three ways to write the regex example in Java. ExampleCompile and RunOutputtrue true true Dot (.) Quantifier in Java RegexIn Java regular expressions, the dot (.) quantifier matches exactly one character, except a newline. It is commonly used when the exact character is not known but its position in the string matters. ExampleConsider the following example to understand the use of the dot (.) quantifier in Java regex. Java Regex Character ClassesJava Regex character classes are used to define a set or group of characters that can match a single character at a specific position in a string. The following table shows different types of character classes in Java Regular Expressions and explains how each character class pattern is used to match specific sets or ranges of characters while performing pattern matching.
ExampleConsider the following example to understand the use of character classes in regular expressions: Java Regex QuantifiersJava Regex Quantifiers define how many times a character, group, or character class can occur in a pattern. They are used to control the repetition of elements while matching strings. The following table shows different types of quantifiers in Java Regular Expressions and explains how each quantifier controls the number of times a character or pattern can occur in a string.
ExampleThe following example demonstrates how different regex quantifiers (?, +, *) work with the character class [amn].
Java Regex MetacharactersJava Regex Metacharacters are special characters that have a predefined meaning in regular expressions and are used to define rules for pattern matching, such as position, repetition, or character types. This table shows commonly used metacharacters in Java Regular Expressions along with their meanings, explaining how each metacharacter is used to match specific character types, positions, or boundaries in a string.
ExampleThe following example demonstrates the use of regex metacharacters \d and \D:.
More Examples on Java RegexPractice the following examples to strengthen your understanding of Java Regular Expressions. These examples cover pattern matching, validation, searching, replacing, and extracting information using regex. Example 1: Alphanumeric String Validation (Fixed Length)Example 2: Mobile Number Validation Using RegexThe following example demonstrates how to validate 10-digit numeric mobile numbers that must start with 7, 8, or 9, using character classes, quantifiers, and metacharacters. Example 3: Java Regex Finder ToolThe following example demonstrates how to search and locate multiple occurrences of a regex pattern within a given text using the Pattern and Matcher classes. 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 Example 4: Java Regex Features DemonstrationThe following example demonstrates various regex operations such as:
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 Example 5: Extracting Data Using Regex PatternsThe following example demonstrates how to extract:
from a given text using regular expressions and the Matcher class. 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 Overall, regular expressions in Java provide a flexible and powerful way to work with strings, allowing you to perform complex matching and manipulation operations with relatively simple patterns. Next TopicJava Exception Handling |
We request you to subscribe our newsletter for upcoming updates.