Home >Java >javaTutorial >Summary of common Java regular expression methods

Summary of common Java regular expression methods

WBOY
WBOYOriginal
2024-01-09 10:09:451219browse

Summary of common Java regular expression methods

Summary of common Java regular expression methods

Regular expression is a powerful tool for matching, finding and replacing strings, which has been introduced in Java Wide range of applications. This article will summarize some commonly used regular expression methods in Java and provide specific code examples.

  1. matches method
    The matches method is used to determine whether a string matches a specified regular expression. It will try to match the entire input sequence against the regular expression, returning true if the match is successful, otherwise false.

Sample code:

String regex = "a*b";
String input = "aab";
boolean isMatched = input.matches(regex);
System.out.println(isMatched); // 输出true
  1. find method
    The find method is used to find the next subsequence in a string that satisfies a regular expression. It searches from the beginning of the string and returns true if a matching subsequence is found, otherwise it returns false.

Sample code:

String regex = "\d+";
String input = "123abc456def";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println(matcher.group()); // 输出123、456
}
  1. replaceAll method
    replaceAll method is used to replace all subsequences that satisfy the regular expression with the specified string.

Sample code:

String regex = "\d+";
String input = "123abc456def";
String replacement = "X";
String output = input.replaceAll(regex, replacement);
System.out.println(output); // 输出XabcXdef
  1. split method
    The split method is used to split a string according to a regular expression and return a string array.

Sample code:

String regex = "[,.\s]+";
String input = "Java,Python,C++,JavaScript";
String[] output = input.split(regex);
for (String word : output) {
    System.out.println(word); // 输出Java、Python、C++、JavaScript
}
  1. Combined use of matches, find, and group methods
    You can use matches, find, and group methods to achieve more complex matching operations. .

Sample code:

String regex = "(\d+)([a-zA-Z]+)";
String input = "123abc456def";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
    // 输出整个字符串
    System.out.println(matcher.group(0)); // 输出123abc456def
    // 输出第一个分组
    System.out.println(matcher.group(1)); // 输出123
    // 输出第二个分组
    System.out.println(matcher.group(2)); // 输出abc
}

The above is a summary and code examples of common methods of Java regular expressions. Mastering these methods can help us handle string matching, search, and replacement tasks more conveniently and efficiently, and improve development efficiency. Of course, the syntax of regular expressions is very rich and complex, and needs to be learned and applied according to specific needs.

The above is the detailed content of Summary of common Java regular expression methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn