SoFunction
Updated on 2025-03-08

Java regular related Pattern and Matcher classes and pitfalls encountered

This article records what I learned and encountered when learning Java regular expressions.

Let’s first talk about the three methods in Matcher (the result is taken as a group() method as an example)

  • matches(): The entire match, only if the entire character sequence is matched completely successfully, will return True, otherwise False will be returned. But if the previous part matches successfully, the position of the next match will be moved. For example, if the string is "a123" and the regular expression is "\w\d\d\d", the matches() method returns true. In other words, the string to be matched must correspond one by one to the regular expression, the letter corresponds to the letter, and the number corresponds to the number, so it is called an exact match, and it is not possible to have a wrong one. (It took me a long time to understand it here, and I didn’t mention it when I searched online. Maybe it was because I didn’t understand enough)
  • find(): Partial match, start from the current position, find a matching substring, and move the next matching position. "Find() traverses the input string forward like an iterator." --Excerpted from "Java Programming Thoughts. Look at the find() method in the following example. The string to match is "a123b", the regular expression is "\d\d\d", and the final output is: 123, so it is called partial match. As long as there is, it returns true.
  • lookingAt(): Partial match, always matches from the first character, no longer matches if the match is successful, and no longer matches if the match fails. The lookingAt() method is between the matches() and find() methods. Partial matching of the lookingAt() method refers to the matching starting from the first bit. If the first bit does not match, it will no longer match and directly return false. If the first bit matches, it will match the second bit, and so on. Example: The string to match is "a123b", the regular expression is "\w\d\d", and the output result is "a12", which is easy to understand, which is to match one by one, and when it matches, it matches the next one. The regular "\w\d\d" represents "alphanumeric number", so the output is just a12.

OK to test the code:

package ;
import ;
import ;
/**
 * @author Author: Brother Ketchup
 * @version Created on August 18, 2016 at 8:47:58 am
 * Class Description: Exercise of regular expressions
 */
public class Regex {
  // Find method test  public static void find(String html) {
    String regex = "\\d\\d\\d";
    Pattern pattern = (regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = (html);
    ("find():");
    if (()) {
      (());
    }
  }
  //matches method test  public static void matches(String html) {
    String regex = "^\\w\\d\\d\\d";
    Pattern pattern = (regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = (html);
    ("matches():");
    if (()) {
      (());
    }
  }
  //LookingAt method test  public static void lookingAt(String html) {
    String regex = "\\w\\d\\d";
    Pattern pattern = (regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = (html);
    ("lookingAt():");
    if (()) {
      (());
    }
  }
  public static void main(String[] args) {
    // Find method test    find("a123b");
    //matches method test    matches("a123");
    //LookingAt method test    lookingAt("a123b");
  }
}

Output result:

    find():123
    matches():a123
    lookingAt():a12

Summary: Regular expressions are not difficult in themselves. When used normally, just write them according to the rules. It took me a long time to understand the three methods of matches, find, and lookingAt in my learning. I have been unable to understand what exactly and partial matches mean. In the end, I found the answer I wanted on *.

The above is my personal understanding of learning. If there are any mistakes, I hope to give you guidance.

(Supplement: If the regular expression Pattern is called multiple times, it is easy to cause problems, such as memory overflow, because the regular expression is compiled every time the Pattern is executed, so it is recommended to precompile the required regular expressions.)

Regular expression learning tutorial:/tutorials/regex/, the author in the article is very detailed and easy to understand.

Summarize

The above is the Java regular-related Pattern and Matcher classes that the editor introduced to you and the pitfalls encountered. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!