Regular Expressions

Java 10 min min read Updated: Mar 31, 2026 Intermediate
Regular Expressions
Intermediate Topic 13 of 14

Regular Expressions

In software development, we often need to search, validate, extract, or replace specific patterns inside text. For example, we may need to check whether an email is valid, find all digits in a sentence, extract dates from logs, or replace unwanted characters in a string. In Java, such pattern-based text processing is done using Regular Expressions.

Regular Expressions, often called Regex, are a very powerful tool for working with strings. They allow developers to define search patterns and apply them to text in a flexible and efficient way.

Key Concept: A regular expression is a pattern used to match, validate, search, extract, or replace text based on specific rules.

What is a Regular Expression?

A regular expression is a sequence of characters that defines a search pattern.

For example:

  • find only digits
  • check whether input contains only letters
  • validate phone number format
  • extract words beginning with a capital letter

In Java, regex support is mainly available through:

java java.util.regex

Important classes:

  • Pattern
  • Matcher

Why Regular Expressions are Important

  • text validation
  • pattern matching
  • data extraction
  • text replacement
  • input sanitization
  • log analysis

Regex is commonly used in:

  • form validation
  • search functionality
  • data cleaning
  • parsing configuration or logs
  • text processing systems

How Regex Works

Regex matches text based on pattern rules. The pattern can define:

  • which characters are allowed
  • how many times they should appear
  • where they should appear
  • what combinations are valid

Basic Regex Example

Suppose we want to check whether a string contains only digits.

java public class Main { public static void main(String[] args) { String text = "12345"; System.out.println(text.matches("\\d+")); } }

Output:

text true

Here:

  • \\d means a digit
  • + means one or more times

Regex in Java Using String Methods

Java provides a simple method:

java matches()

This method checks whether the entire string matches the given regex pattern.

Example

java String email = "test123"; System.out.println(email.matches("[a-zA-Z0-9]+"));

This checks whether the full string contains only letters and digits.

Main Regex Classes in Java

1. Pattern

The Pattern class represents a compiled regular expression.

2. Matcher

The Matcher class is used to match the compiled pattern against a string.

Example Using Pattern and Matcher

java import java.util.regex.Pattern; import java.util.regex.Matcher; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("Java"); Matcher matcher = pattern.matcher("I am learning Java"); System.out.println(matcher.find()); } }

Output:

text true

Here:

  • Pattern.compile() creates regex pattern
  • matcher() applies it to a string
  • find() checks whether the pattern exists anywhere in the string

Common Regex Symbols

Regex uses many special symbols called metacharacters.

Symbol Meaning
. Any single character
\\d Any digit
\\D Any non-digit
\\w Word character (letter, digit, underscore)
\\W Non-word character
\\s Whitespace
\\S Non-whitespace
[] Character class
^ Start of line/string
$ End of line/string

Character Classes

Character classes define a set of allowed characters.

Examples

java [a-z] // lowercase letters [A-Z] // uppercase letters [0-9] // digits [a-zA-Z] // any letter [a-zA-Z0-9] // letters and digits

Example

java System.out.println("Java123".matches("[a-zA-Z0-9]+"));

Quantifiers

Quantifiers define how many times a character or group should appear.

Quantifier Meaning
? 0 or 1 time
* 0 or more times
+ 1 or more times
{n} Exactly n times
{n,} At least n times
{n,m} Between n and m times

Examples

java \\d+ // one or more digits [a-z]{3} // exactly 3 lowercase letters \\w{5,10} // word characters between 5 and 10 length

Anchors

Anchors define position in the string.

  • ^ → start of string
  • $ → end of string

Example

java System.out.println("Java".matches("^Java$"));

This means the whole string must be exactly "Java".

Common Regex Examples

1. Check Only Digits

java System.out.println("12345".matches("\\d+"));

2. Check Only Alphabets

java System.out.println("Java".matches("[a-zA-Z]+"));

3. Check Alphanumeric

java System.out.println("Java123".matches("[a-zA-Z0-9]+"));

4. Validate Mobile Number (10 digits)

java System.out.println("9876543210".matches("\\d{10}"));

5. Validate Basic Email Format

java String email = "user123@gmail.com"; System.out.println(email.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"));

This is a common regex-style email validation pattern.

Matcher Class Methods

The Matcher class provides important methods such as:

  • find()
  • matches()
  • group()
  • start()
  • end()

find()

Finds whether the pattern exists anywhere in the input string.

java Pattern p = Pattern.compile("Java"); Matcher m = p.matcher("I love Java programming"); System.out.println(m.find());

matches()

Checks whether the entire string matches the pattern.

java Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher("12345"); System.out.println(m.matches());

group()

Returns the matched text after a successful find.

java Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher("Age is 25"); if (m.find()) { System.out.println(m.group()); }

Output:

text 25

start() and end()

These methods return the start and end positions of a match.

java Pattern p = Pattern.compile("Java"); Matcher m = p.matcher("I love Java"); if (m.find()) { System.out.println(m.start()); System.out.println(m.end()); }

Regex Replacement in Java

Regex can also be used to replace text.

replaceAll()

java String text = "Java 123 Python 456"; String result = text.replaceAll("\\d+", "#"); System.out.println(result);

Output:

text Java # Python #

replaceFirst()

java String text = "123 Java 456"; System.out.println(text.replaceFirst("\\d+", "#"));

Splitting Strings with Regex

The split() method also supports regex.

java String text = "Java,Python,C++,Go"; String[] parts = text.split(","); for (String s : parts) { System.out.println(s); }

Split with Multiple Delimiters

java String text = "Java,Python;C++ Go"; String[] parts = text.split("[,; ]"); for (String s : parts) { if (!s.isEmpty()) { System.out.println(s); } }

Escaping Special Characters

Some characters are special in regex, such as:

  • .
  • *
  • +
  • ?
  • [ ]
  • ( )
  • { }
  • \\

If you want to match them literally, they must be escaped.

Example

java System.out.println("a.b".matches("a\\.b"));

Without escaping, . means any character.

Real-World Use Cases

1. Email Validation

java String email = "user@gmail.com"; boolean valid = email.matches("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"); System.out.println(valid);

2. Password Strength Pattern

Example requirement:

  • at least 8 characters
  • contains letters and digits
java String password = "Java1234"; boolean valid = password.matches("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"); System.out.println(valid);

3. Extract Numbers from Text

java import java.util.regex.*; public class Main { public static void main(String[] args) { Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher("Order 123 and invoice 456"); while (m.find()) { System.out.println(m.group()); } } }

4. Remove Extra Spaces

java String text = "Java is powerful"; System.out.println(text.replaceAll("\\s+", " "));

Greedy Matching Concept

Many regex patterns are greedy, meaning they try to match as much text as possible.

Example:

java String text = "OneTwo"; System.out.println(text.replaceAll("<.*>", "#"));

This may match more than expected because .* is greedy.

A non-greedy form uses .*?.

Common Mistakes

  • forgetting that matches() checks the whole string
  • confusing find() with matches()
  • not escaping special characters properly
  • writing overly complex regex without testing
  • expecting regex to solve every text problem elegantly

Best Practices

  • start with simple patterns and build gradually
  • test regex with sample inputs
  • use Pattern and Matcher for more control
  • escape special characters carefully
  • keep regex readable when possible

Interview-Oriented Points

  • Regex is used for text pattern matching and validation
  • Java regex classes are Pattern and Matcher
  • matches() checks the whole string
  • find() searches for occurrence inside the string
  • group() returns the matched text
  • replaceAll() replaces all matches
  • \\d means digit, \\s means whitespace, \\w means word character
  • Character classes and quantifiers are key parts of regex

Conclusion

Regular expressions are one of the most powerful tools in Java string processing. They allow developers to validate, search, extract, split, and replace text using compact pattern-based rules.

Although regex can look complex at first, once the symbols and rules are understood clearly, it becomes a very useful skill for real-world Java development, especially in validation and text-processing tasks.

Quick Summary: Regular expressions in Java are used for pattern-based text matching, validation, extraction, and replacement through classes like Pattern and Matcher.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators