Regular Expressions in Java with Examples: For Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Regular Expressions in Java: A Complete Guide for Beginners



Regular expressions (regex) are a powerful tool for searching, extracting, and replacing text based on patterns. In Java, working with regular expressions is implemented through the java.util.regex package. In this article, we will cover the basics of regular expressions in Java, learn how to use the Pattern and Matcher classes, and look at practical examples.



1. Main Classes for Working with regex in Java



In Java, two main classes are used for working with regular expressions:



  • Pattern — represents a compiled regular expression.
  • Matcher — performs search and matching operations on text.


There is also a convenient method String.matches(), which allows you to quickly check if a string matches a pattern, but it is less flexible.



Example 1: Simple Match



import java.util.regex.*;

public class RegexExample1 { public static void main(String[] args) { String text = "Hello, world! Java is the best language."; String pattern = "Java";

Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text);

if (m.find()) { System.out.println("Word found: " + m.group()); } else { System.out.println("No matches found"); } }}


Output: Word found: Java



2. Metacharacters and Character Classes



Regular expressions use special characters (metacharacters) to describe patterns:



  • . — any character (except line break)
  • \\d — digit [0-9]
  • \\D — non-digit
  • \\w — letter, digit, or underscore [a-zA-Z0-9_]
  • \\W — non-word character
  • \\s — whitespace character
  • \\S — non-whitespace character
  • [abc] — one of the characters a, b, or c
  • [^abc] — any character except a, b, c


Example 2: Finding Digits in Text



import java.util.regex.*;

public class RegexExample2 { public static void main(String[] args) { String text = "My number: 123-456-7890"; String pattern = "\\\\d+"; // one or more digits

Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text);

while (m.find()) { System.out.println("Number found: " + m.group()); } }}


Output:

Number found: 123Number found: 456Number found: 7890


3. Quantifiers and Boundaries



Quantifiers specify how many times a pattern element should repeat:



  • * — 0 or more times
  • + — 1 or more times
  • ? — 0 or 1 time
  • {n} — exactly n times
  • {n,} — n or more times
  • {n,m} — from n to m times


Boundaries help specify where the pattern should be located:



  • ^ — start of string
  • $ — end of string
  • \\b — word boundary


Example 3: Email Address Validation



import java.util.regex.*;

public class RegexExample3 { public static void main(String[] args) { String email = "user@example.com"; String pattern = "^[\\\\w.-]+@[\\\\w.-]+\\\\.\\\\w{2,}$";

boolean isValid = Pattern.matches(pattern, email); System.out.println("Email is valid: " + isValid); }}


Output: Email is valid: true



4. Capturing Groups and Text Replacement



Capturing groups allow you to extract pa

Blogs

Book Recommendations