Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java RegEx

Regular expressions, also known as RegEx, are a powerful tool for searching and manipulating text. Java provides support for RegEx through the java.util.regex package. In this article, we will explore the basics of Java RegEx and how to use it in your Java applications.

Brief Explanation of Java RegEx

Java RegEx is a pattern matching engine that allows you to search for specific patterns in text. It uses a syntax that is similar to other programming languages, but with some differences. The basic syntax for a RegEx pattern in Java is:

Pattern pattern = Pattern.compile("pattern");
Matcher matcher = pattern.matcher("text");

The Pattern class is used to compile the RegEx pattern, and the Matcher class is used to match the pattern against a given text. The Pattern.compile() method takes a string argument that represents the RegEx pattern. The Matcher.matcher() method takes a string argument that represents the text to be searched.

Java RegEx supports a wide range of pattern matching features, including:

  • Character classes
  • Quantifiers
  • Grouping and capturing
  • Boundary matchers
  • Backreferences
  • Lookahead and lookbehind

Code Examples

Let's take a look at some code examples to see how Java RegEx works in practice.

Matching a Phone Number

Suppose we want to match a phone number in the format of (XXX) XXX-XXXX. We can use the following RegEx pattern:

Pattern pattern = Pattern.compile("\\(\\d{3}\\) \\d{3}-\\d{4}");
Matcher matcher = pattern.matcher("(123) 456-7890");
boolean matchFound = matcher.find();

In this example, we use the \\( and \\) escape sequences to match the parentheses, and the \\d character class to match digits. The {3} and {4} quantifiers are used to match exactly three and four digits, respectively. The Matcher.find() method is used to find the first occurrence of the pattern in the text.

Replacing Text

We can also use Java RegEx to replace text in a string. Suppose we want to replace all occurrences of the word "apple" with "orange" in a given text. We can use the following code:

String text = "I like to eat apples. Apples are my favorite fruit.";
String replacedText = text.replaceAll("apple", "orange");

In this example, we use the String.replaceAll() method to replace all occurrences of the word "apple" with "orange". The first argument is the RegEx pattern to match, and the second argument is the replacement text.

Splitting Text

We can also use Java RegEx to split a string into an array of substrings. Suppose we want to split a comma-separated list of names into an array of individual names. We can use the following code:

String names = "John, Jane, Bob, Alice";
String[] nameArray = names.split(", ");

In this example, we use the String.split() method to split the string into an array of substrings. The argument is the RegEx pattern to use as the delimiter.

Conclusion

Java RegEx is a powerful tool for searching and manipulating text in Java applications. It provides a wide range of pattern matching features and can be used for tasks such as searching for specific patterns, replacing text, and splitting strings. By understanding the basics of Java RegEx, you can add powerful text processing capabilities to your Java applications.

References

Activity