Regular expressions (regex) are powerful tools for searching and matching text patterns. They’re commonly used to find specific patterns in strings, such as email addresses, phone numbers, or any other structured text. In this lesson, we will:
- Learn the basics of regular expressions for beginners.
- Explore how regular expressions work with Java’s
matches()method for validating strings.
What is a Regular Expression?
At its core, a regular expression is a special sequence of characters that helps define a search pattern. Regular expressions can be used to match strings or parts of strings based on specific rules.
In Java, the String class has a method called matches() that lets you check whether a string matches a regular expression pattern.
Basic Syntax of Regular Expressions
A regular expression in Java is typically made up of a combination of literal characters and special symbols that define the pattern you are looking for. Here are a few key components of regular expressions:
Direct String Comparison
At its most basic, you can use matches() to compare two strings directly for an exact match:
"one".matches("one"); // true
"one".matches("one|two"); // true (because "one" matches one of the alternatives)
"one"matches"one"exactly."one|two"means the string can be either “one” or “two”, so it matches “one”.
Using Character Sets and Ranges
In regular expressions, you can define a set of characters to match using square brackets []. You can also define ranges, like [a-z], to match specific groups of characters.
For example, if you want to check if a string contains only letters (both lowercase and uppercase), you can use the following regex:
"[a-zA-Z]+" // Matches one or more alphabetic characters
Example:
"abc".matches("[a-zA-Z]+"); // true (matches because it's made up of alphabetic characters)
"abc123".matches("[a-zA-Z]+"); // false (contains digits, so it doesn't match)
The + symbol means “one or more” of the preceding characters. So, [a-zA-Z]+ will match any string made up of one or more letters.
Common Regex Symbols
Here are some common regex symbols and their meanings:
| Symbol | Meaning | Example |
|---|---|---|
. | Matches any single character | "a".matches("."); // true |
^ | Anchors the match to the start | "^abc".matches("abc"); // false |
$ | Anchors the match to the end | "abc$".matches("abc"); // false |
\\d | Matches any digit (0-9) | "123".matches("\\d+"); // true |
\\w | Matches any letter, number, or underscore | "abc_123".matches("\\w+"); // true |
[abc] | Matches any one of a, b, or c | "b".matches("[abc]"); // true |
[^abc] | Matches any character except a, b, or c | "d".matches("[^abc]"); // true |
* | Matches 0 or more occurrences | "aaaa".matches("a*"); // true |
+ | Matches 1 or more occurrences | "aaa".matches("a+"); // true |
{n} | Matches exactly n occurrences | "aaaa".matches("a{4}"); // true |
{n,m} | Matches between n and m occurrences | "abc".matches("[a-z]{3,5}"); // true |
Examples with String.matches()
Let’s see how matches() can be used with different regular expressions.
Match any single character
"A".matches("."); // true
Match a string of digits (one or more):
"12345".matches("\\d+"); // true
Match exactly four digits
"1234".matches("\\d{4}"); // true
Match a combination of digits and letters
"1234four".matches("\\d+\\w+"); // true
In the example above, the regex \\d+ matches one or more digits, and \\w+ matches one or more word characters (letters, numbers, or underscores).
More Complex Examples
Match a valid email address
"example@domain.com".matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$");
Match a phone number (e.g., 123-456-7890)
"123-456-7890".matches("\\d{3}-\\d{3}-\\d{4}");
Summary
- Regular Expressions (Regex ) are a pattern used to search and validate strings in Java
- Regular Expressions are useful for tasks like validating emails, phone numbers and more
- The
String.matches()is a useful method for checking if a string matches a regex pattern - Symbols like
\\d,*, and{n,m}define search rules in regex - Character ranges (e.g.,
[a-zA-Z]) help to define pattern searches
Further Learning Resources
For deeper insights into regular expressions and more advanced techniques, here are some useful resources:
- Geeks for Geeks – Regular Expressions in Java
- Vogella – Tutorial on Java Regular Expressions
- RegexOne – Learn Regex Interactively
Leave a Reply