zoukankan      html  css  js  c++  java
  • JAVA笔记28-正则表达式(补充、不重要)

    一、Greedy(贪婪的)尽可能多的匹配,Reluctant(不情愿的)尽可能少的匹配。Possessive(独占的)不常用。

    Greedy 数量词
    X? X,一次或一次也没有
    X* X,零次或多次
    X+ X,一次或多次
    X{n} X,恰好 n 次
    X{n,} X,至少 n 次
    X{n,m} X,至少 n 次,但是不超过 m 次
     
    Reluctant 数量词
    X?? X,一次或一次也没有
    X*? X,零次或多次
    X+? X,一次或多次
    X{n}? X,恰好 n 次
    X{n,}? X,至少 n 次
    X{n,m}? X,至少 n 次,但是不超过 m 次
     
    Possessive 数量词
    X?+ X,一次或一次也没有
    X*+ X,零次或多次
    X++ X,一次或多次
    X{n}+ X,恰好 n 次
    X{n,}+ X,至少 n 次
    X{n,m}+ X,至少 n 次,但是不超过 m 次

    例如:

    import java.util.regex.*;
    public class Test{
        public static void main(String args[]){
            //Pattern p = Pattern.compile("(.{3,10})[0-9]");//输出0 9
            //Pattern p = Pattern.compile("(.{3,10}?)[0-9]");//输出0 5
            Pattern p = Pattern.compile("(.{3,10}+)[0-9]");//输出not match!
            String s = "aaaa5bbb6";
            Matcher m = p.matcher(s);
            if(m.find()){
                System.out.println(m.start()+" "+m.end());
            }
            else{
                System.out.println("not match!");
            }
        }
    }

    二、除了?=,其他一般不用

    Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
    等价于:Pattern p = Pattern.compile("(?i)java");

    特殊构造(非捕获)
    (?:X) X,作为非捕获组
    (?idmsux-idmsux)  Nothing,但是将匹配标志i d m s u x on - off
    (?idmsux-idmsux:X)   X,作为带有给定标志 i d m s u x on - off
    (?=X) X,通过零宽度的正 lookahead
    (?!X) X,通过零宽度的负 lookahead
    (?<=X) X,通过零宽度的正 lookbehind
    (?<!X) X,通过零宽度的负 lookbehind
    (?>X) X,作为独立的非捕获组
    import java.util.regex.*;
    public class Test{
        public static void main(String args[]){
            Pattern p = Pattern.compile(".{3}");//输出444 a66
            //Pattern p = Pattern.compile(".{3}(?=a)");//输出444
            //Pattern p = Pattern.compile("(?=a).{3}");//输出a66
            //Pattern p = Pattern.compile(".{3}(?!a)");//输出44a 66b
            //Pattern p = Pattern.compile("(?!a).{3}");//输出444 66b
            //Pattern p = Pattern.compile(".{3}(?<!a)");//输出444 a66
            //Pattern p = Pattern.compile(".{3}(?<=a)");//输出44a
            String s = "444a66b";
            Matcher m = p.matcher(s);
            while(m.find()){
                System.out.println(m.group());
            }
        }
    }

    三、

    Back 引用
    n 任何匹配的 nth 捕获组
    import java.util.regex.*;
    public class Test{
        public static void main(String args[]){
            Pattern p = Pattern.compile("(\d\d)\1");//与group(1)相同
            String s = "1212";
            Matcher m = p.matcher(s);
            System.out.println(m.matches());//true
            
            p = Pattern.compile("(\d(\d))\2");//与group(2)相同
            s = "12";
            m = p.matcher(s);
            System.out.println(m.matches());//false
    
            p = Pattern.compile("(\d(\d))\2");//与group(2)相同
            s = "122";
            m = p.matcher(s);
            System.out.println(m.matches());//true
        }
    }
  • 相关阅读:
    Objective-C 和 Swift 第三方库使用
    如何在Swift的代码中使用OC的代码, 在OC的代码中使用Swift的代码?
    swift之水纹动画
    HandyJSON.Metadata.Class Xcode10.2, swift5.0 报错 linker command failed with exit code 1
    Python开发【第十篇】:CSS (二)
    Python开发【第十篇】:CSS --无内容点击-不进去(一)
    Python开发【第九篇】:HTML (二)
    Python开发【第九篇】:HTML --无内容点击-不进去(一)
    Python开发【第八篇】:网络编程
    Python开发【第七篇】:面向对象 和 python面向对象(初级篇)(上)
  • 原文地址:https://www.cnblogs.com/seven7seven/p/3692496.html
Copyright © 2011-2022 走看看