About the use of ^ and $ in java regular expressions

Among the boundary matching characters of java regular expressions, there are two commonly used characters: "^" and "$", these two characters are more confusing to understand. Let's talk about the meaning of these two characters:
"^": match the starting position of the input string. If the Multiline property of the RegExp object is set, ^ will also match the position after "\n" or "\r" (ie, the beginning of each line);

" $ " : Matches the position at the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r" (ie, the end of each line).

Many beginner friends do not understand the usage of these two characters. There seems to be no difference between "^aa+ $ " and "aa+" when matching. When we look at the code, it is easy to understand.
Let's look at the last part of the code.
Strings a and b, strings and matching regular expressions are basically the same, except that there is a "^" character in b. Then we look at the output string result. It is not difficult to find that in a, the parts of the two numbers are replaced with "Z", and in b, only the first two numbers are replaced with "Z", because " ^" is required to match from the beginning. In this way, the two numbers in the middle will not match, and there will be no replacement. For a without "^", as long as the two numbers are satisfied, matching and replacement can be performed, regardless of whether the matching is performed from the beginning of the string. In the same way, the "$" judgment is to match from the end, which can be understood by analogy with the results of c and d.
It is worth mentioning that, whether it is a regular expression with "^" and "\$", when the matches method is matched, it returns false. Because matches() matches the entire string, not a part, it is obvious that the regular expression used in the text cannot completely match the input string, so the return value is false.

public class RegexDemo {

    public static void main(String[] args) {
        //匹配邮箱
        String regex="^.+@.+(\\..+){1,}$";
        String str="[email protected]";
//      System.out.println(str.matches(regex));

        //匹配固定电话  4位区号-7位号码 或者 3位区号-8位号码
        String regex2="^\\d{4}-\\d{7}|\\d{3}-\\d{8}$";
        String str2="020-13222113";
        String str3="0532-9989211";
//      System.out.println(str2.matches(regex2));
//      System.out.println(str3.matches(regex2));

        //匹配除了a和9之外的数字或字符
        String regex3="^[^9a]{1,}$";
        String str4="1234fsfse";
        String str5="a2343";
//      System.out.println(str4.matches(regex3));
//      System.out.println(str5.matches(regex3));

        //^和$的用法
        String a = "3131sasfasd".replaceAll("\\d{2}", "Z");
        String b = "3131sasfasd".replaceAll("^\\d{2}", "Z");//仅替换字符串开头的两个数字
        String c = "3131sdasfasd".replaceAll("sd", "Z");
        String d = "3131sdsfasd".replaceAll("sd$", "Z");//仅替换字符串开头的两个数字
        System.out.println(a);//ZZZsasfasd
        System.out.println(b);//Z3131sasfasd
        System.out.println(c);//aa3131ZasfaZ
        System.out.println(d);//aa3131sdsfaZ
        String str6 = "aa3131sasfasd";
        System.out.println(str6.matches("\\d{2}")); //false
        System.out.println(str6.matches("^\\d{2}"));//false
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325905675&siteId=291194637