JavaRegex

來源:互聯網
上載者:User

標籤:Regex   java   pattern   matcher   

現在有一個字串: [“1002201504303120”,”1002201504253100”,”1002201504213080”],提取數字串到List中

  • 解法一:
import java.util.*;public class Test1{    public static void main(String[] args)     {        String str = "[\"1002201504303120\",\"1002201504253100\",\"1002201504213080\"]";        String[] s = str.split("\"");        List<String> list = new ArrayList<String>();        for(int i = 0; i < s.length ; i ++){            if(s[i].length() > 1){                list.add(s[i]);            }        }        System.out.println(list);    }}

上述解法很簡單,思想也很簡單,接下來看看Regex處理。

  • 解法二
import java.util.*;public class Test1{    public static void main(String[] args)     {        String str = "[\"1002201504303120\",\"1002201504253100\",\"1002201504213080\"]";        String[] s = str.split("[^0-9]");        List<String> list = new ArrayList<String>();        for(int i = 0; i < s.length ; i ++){            if(s[i].length() != 0){                list.add(s[i]);            }        }        System.out.println(list);    }}

通過上述例子,可知String的split方法支援Regex

  • 解法三
public static void main(String[] args) {        String str = "[\"1002201504303120\",\"1002201504253100\",\"1002201504213080\"]";        Pattern p = Pattern.compile("\\d{16}");        Matcher matcher = p.matcher(str);        List<String> list = new ArrayList<>();        while(matcher.find()){            String strVal = matcher.group();            list.add(strVal);        }        if(!list.isEmpty()){            for (String s : list) {                System.out.println(s);            }        }    }
Java中的Regex

java.util.regex:用於匹配字元序列與Regex指定模式的包。包含兩個類,Matcher和Pattern。

Matcher:通過解釋 Pattern 對 character sequence 執行匹配操作的引擎。
Pattern:Regex的編譯表示形式。
PatternSyntaxException : 拋出未經檢查的異常,表明Regex模式中的語法錯誤。

Pattern

Pattern:Regex的編譯表示形式。
指定為字串的Regex必須首先被編譯為此類的執行個體。然後,可將得到的模式用於建立 Matcher 對象,依照Regex,該對象可以與任一字元序列匹配。執行匹配所涉及的所有狀態都駐留在匹配器中,所以多個匹配器可以共用同一模式。

典型的調用順序:
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();

方法
  • static Pattern compile(String regex)
    將給定的Regex編譯到模式中。
  • static Pattern compile(String regex, int flags)
    將給定的Regex編譯到具有給定標誌的模式中。
  • int flags()
    返回此模式的匹配標誌。
  • Matcher matcher(CharSequence input)
    建立匹配給定輸入與此模式的匹配器。
  • static boolean matches(String regex, CharSequence input)
    編譯給定Regex並嘗試將給定輸入與其匹配。
  • String pattern()
    返回在其中編譯過此模式的Regex。
Matcher

通過解釋 Pattern 對 character sequence 執行匹配操作的引擎。

通過調用模式的 matcher 方法從模式建立匹配器。建立匹配器後,可以使用它執行三種不同的匹配操作:
1、matches 方法嘗試將整個輸入序列與該模式比對。
2、lookingAt 嘗試將輸入序列從頭開始與該模式比對。
3、find 方法掃描輸入序列以尋找與該模式比對的下一個子序列。

方法
  • boolean find()
    嘗試尋找與該模式比對的輸入序列的下一個子序列。
  • boolean find(int start)
    重設此匹配器,然後嘗試尋找匹配該模式、從指定索引開始的輸入序列的下一個子序列。
  • String group()
    返回由以前匹配操作所匹配的輸入子序列。
  • String group(int group)
    返回在以前匹配操作期間由給定組捕獲的輸入子序列。
  • int groupCount()
    返回此匹配器模式中的擷取的群組數。
  • boolean lookingAt()
    嘗試將從地區開頭開始的輸入序列與該模式比對。
  • boolean matches()
    嘗試將整個地區與模式比對。
  • Pattern pattern()
    返回由此匹配器解釋的模式。

JavaRegex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.