Java Regex
Java Regex is a powerful tool used for pattern matching. Pattern matching is the process of searching for a specific pattern within a string. Pattern matching can be used to search for specific words, dates, or any other format within a larger text. In Java, we have the java.util.regex package that provides classes for pattern matching with regular expressions.
Syntax
String pattern = "regex pattern";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(stringToMatch);
The pattern is the regular expression we want to search for in a string. We create a compiled pattern from this pattern using the Pattern.compile()
method. Once we have a compiled pattern, we can create a matcher object for the string we want to search using the compiledPattern.matcher()
method.
Example
Let's say we want to search for all phone numbers in a string. We can create the following regular expression:
String pattern = "\\d{3}-\\d{3}-\\d{4}";
This regular expression matches any digits of length three separated by a hyphen(-) and followed by four digits.
Pattern compiledPattern = Pattern.compile(pattern);
String text = "John Smith, 123-456-7890, 456-123-7890";
Matcher matcher = compiledPattern.matcher(text);
We can use the matcher object to find the phone numbers in the text string.
while (matcher.find()) {
System.out.println("Phone number: " + matcher.group());
}
This will print the following output:
Phone number: 123-456-7890
Phone number: 456-123-7890
Explanation
In the example above, we have created a regular expression that matches phone numbers of the format XXX-XXX-XXXX
.
\\d{3}
matches any digit (0-9) of length 3.-
matches a hyphen (-).{3}
specifies that the previous pattern (\d) should be matched exactly 3 times.
The while loop executes as long as the matcher finds a match. matcher.group()
returns the currently matched string.
Use
Java Regex can be used for various pattern matching operations in a string. Some common use cases of Java Regex are:
- Validating user inputs such as passwords, emails, phone numbers, etc.
- Extracting information from a large text, such as dates, URLs, and phone numbers.
- Replacing characters or words in a string based on a specific pattern.
Important Points
- Regular expressions in Java are specified using the
java.util.regex
package. - The
Pattern
class is used to represent a compiled regular expression, while theMatcher
class is used to match a compiled regular expression against a string. - Special characters such as
^
,$
,\
,?
,*
,+
,{}
,[]
have special meaning in regular expressions and need to be escaped using the backslash () character. - Regular expressions are powerful but can also be complex and difficult to read/debug. It is important to test your regular expressions thoroughly.
Summary
Java Regex is a powerful tool for pattern matching in Java. It provides a simple, yet powerful way to search for patterns in a string using regular expressions. Regular expressions can be complex and difficult to read, but with practice, they can be a powerful tool in your Java programming toolkit.