java-Regex

來源:互聯網
上載者:User

標籤:find   end   range   分組   --   amp   mat   產生   文法   

1、什麼是Regex

定義了一組尋找模式的有序字串

2、Regex的基礎文法有哪些

單匹配單個字元

. 任一字元

\d 數字[0-9]

\D 非數字[^0-9]

\s 空白字元[\t\n\x0b\f\r]

\w 一個字元[a-zA-Z_0-9]

範圍匹配單個字元

[abc]         simple class

[^abc]        negation

[a-zA-Z]  range

[a-d[m-p]]  [a-dm-p] union

[a-z&&[def]]  [def] intersection

[a-z&&[^bc]]  [ad-z] subtraction

[a-z&&[^m-p]]  [a-lq-z] subtration

---|匹配多個字元 貪婪模式

? 0個或1個

* 任意多個相同字元(0或多個) "".matches("m*")

+ 1個或多個字元

X{n} =n

X{n,} >=n

X{n,m} <=n <=m

--|邊界匹配

^abc   start with "abc"  [^abc] not "a"、"b"、"c"

abc$  end with "abc"

3、java怎麼使用Regex

Pattern類 Matcher類

Pattern p = Pattern.compile()方法 //先把Regex產生一個Pattern對象

Matcher m = p.matcher()方法  //通過Pattern對象matcher方法把尋找的內容產生一個Mathcher對象

m.matches()方法

m.find()方法  //順序匹配

m.group()方法  //通過find後,就可以group擷取

m.reset()方法

m.start()方法

m.end()方法

4、正則表達的高階用法

分組 括弧 數左括弧

貪婪模式  .* 

非貪婪模式  .*?

 

java-Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.