首页 > 代码库 > 在java中使用正则表达式注意的地方

在java中使用正则表达式注意的地方

1、 对^与$的理解

通常我们会通过类似Matcher matcher = Pattern.compile(regex).matcher(string);的代码去拿到一个Matcher对象。
这种情况下regex中的^与$匹配的是整个待匹配串string的开头与结尾。
而要使^与$去匹配每一行的开始与结尾,则要使用Pattern.MULTILINE。即:Matcher matcher = Pattern.compile(regexPattern.MULTILINE).matcher(string);

看下面的几个例子:

        String inputStr = "stg换行前\r\n换stg行后";
        System.out.println("-----------------");
        System.out.println(inputStr);
        System.out.println("-----------------");
        Matcher matcher = Pattern.compile(".*stg.*").matcher(inputStr);
        while(matcher.find()){
            System.out.println("start-" + matcher.start());
            System.out.println("end-" + matcher.end());
            System.out.println("matches-" + matcher.group());
        }                            

 

匹配结果:

-----------------
stg换行前
换stg行后
-----------------
start-0
end-6
matches-stg换行前
start-8
end-14
matches-换stg行后

 

如果我们将正则换成"^.*stg.*",则只能匹配到一次:

        String inputStr = "stg换行前\r\n换stg行后";
        System.out.println("-----------------");
        System.out.println(inputStr);
        System.out.println("-----------------");
        Matcher matcher = Pattern.compile("^.*stg.*").matcher(inputStr);
        while(matcher.find()){
            System.out.println("start-" + matcher.start());
            System.out.println("end-" + matcher.end());
            System.out.println("matches-" + matcher.group());
        }

匹配结果:

-----------------
stg换行前
换stg行后
-----------------
start-0
end-6
matches-stg换行前

 

如果将正则换成".*stg.*$",也只能得到一次匹配:

 

        String inputStr = "stg换行前\r\n换stg行后";
        System.out.println("-----------------");
        System.out.println(inputStr);
        System.out.println("-----------------");
        Matcher matcher = Pattern.compile(".*stg.*$").matcher(inputStr);
        while(matcher.find()){
            System.out.println("start-" + matcher.start());
            System.out.println("end-" + matcher.end());
            System.out.println("matches-" + matcher.group());
        }

 

匹配结果:

-----------------
stg换行前
换stg行后
-----------------
start-8
end-14
matches-换stg行后

 

如果我们将正则换成:"^.*stg.*$",将得不到任何匹配。因为.(点)匹配不到换行符(.匹配的是除换行符以外的任意字符)。

 

        String inputStr = "stg换行前\r\n换stg行后";
        System.out.println("-----------------");
        System.out.println(inputStr);
        System.out.println("-----------------");
        Matcher matcher = Pattern.compile("^.*stg.*$").matcher(inputStr);
        while(matcher.find()){
            System.out.println("start-" + matcher.start());
            System.out.println("end-" + matcher.end());
            System.out.println("matches-" + matcher.group());
        }

 

匹配结果:

-----------------
stg换行前
换stg行后
-----------------

 

如果要让^与$匹配每行的开始与结束,则要将代码改成:Pattern.compile("^.*stg.*$", Pattern.MULTILINE).matcher(inputStr);

 

        String inputStr = "stg换行前\r\n换stg行后";
        System.out.println("-----------------");
        System.out.println(inputStr);
        System.out.println("-----------------");
        Matcher matcher = Pattern.compile("^.*stg.*$", Pattern.MULTILINE).matcher(inputStr);
        while(matcher.find()){
            System.out.println("start-" + matcher.start());
            System.out.println("end-" + matcher.end());
            System.out.println("matches-" + matcher.group());
        }

 

匹配结果:

-----------------
stg换行前
换stg行后
-----------------
start-0
end-6
matches-stg换行前
start-8
end-14
matches-换stg行后