1. 程式人生 > >正則表達式相關

正則表達式相關

print compile div matcher out 則表達式 tr1 輸出 match

第一次使用正則表達式,利用正則表達式中group的方法在字符串str1[201701, 201706]中取出201701,201706,

    String str1 = "[201701, 201706]";
    Pattern pattern = Pattern.compile("\\[(\\d{6}),\\s(\\d{6})\\]");
    Matcher match = pattern.matcher(str1);
    match.find();
    String start = match.group(1);
    String end = match.group(2);
    System.out.println(start);
    System.out.println(end);

此時 輸出:

201701
201706

\\[(\\d{6}),\\s(\\d{6})\\]中 第一個()內的內容為group(1),第二個()中的內容為group(2)

match.find()必須要有

正則表達式相關