【The Java? Tutorials】這個(gè)系列的博客是閱讀Java官方教程做的筆記礼患。關(guān)于Java的教程惕澎,網(wǎng)上有各種各樣的資源槐瑞,有很多總結(jié)得很好的博客据过,但是我覺得都沒有看官方的教程來得直接、靠譜和全面屑宠。
【Regular Expressions】這個(gè)子系列是關(guān)于Java的正則表達(dá)式厢洞,介紹如何使用java.util.regex
包中的API。主要包含以下幾部分:
1. Introduction
介紹Java中正則表達(dá)式的概念典奉,以及一些主要的類躺翻。
2. Test Harness
一個(gè)簡(jiǎn)單的程序,用于測(cè)試如何用正則表達(dá)式進(jìn)行模式匹配卫玖。
3. String Literals
介紹一些基礎(chǔ)概念:模式匹配(pattern matching)公你、元字符(metacharacters)和引用(quoting)。
4. Character Classes
介紹character classes的概念:simple character classes, negation, ranges, unions, intersections and subtraction假瞬。
5. Predefined Character Classes
介紹一些基礎(chǔ)的預(yù)定義的character classes:whitespace, word and digit characters陕靠。
6. Quantifiers
介紹量詞:greedy vs. reluctant vs. possessive
7. Capturing Groups
介紹分組:將多個(gè)字符當(dāng)成是一個(gè)整體迂尝。
8. Boundary Matchers
介紹匹配限定:line, word等
9. Methods of the Pattern Class
介紹Pattern
類中一些有用的方法。
10. Methods of the Matcher Class
介紹Matcher
類中一些有用的方法剪芥。
11. Methods of the PatternSytaxException Class
介紹如何檢查和處理PatternSytaxException
異常垄开。
12. Addional Resources
介紹一些其他的關(guān)于正則表達(dá)式的學(xué)習(xí)資源。
什么是正則表達(dá)式
Regular expressions are a way to describe a set of strings based on common characteristics shared by each string in the set.
不同的編程語(yǔ)言有不同的正則表達(dá)式語(yǔ)法粗俱,Java的正則表達(dá)式語(yǔ)法與Perl的最像说榆。
Java中如何表示正則表達(dá)式
在java.util.regex
中,有三個(gè)主要的類:Pattern
, Matcher
和PatternSyntaxException
寸认。
- A Pattern object is a compiled representation of a regular expression.
Pattern
沒有提供public的構(gòu)造器,要?jiǎng)?chuàng)造一個(gè)Pattern
的實(shí)例串慰,要調(diào)用Pattern
提供的public static方法compile偏塞。compile接收一個(gè)字符串類型的參數(shù),表示正則表達(dá)式邦鲫。關(guān)于正則表達(dá)式的語(yǔ)法后面會(huì)介紹灸叼。 - A Matcher object is the engine that interprets the pattern and performs match operations against an input string. 和
Pattern
一樣,Matcher
沒有提供public的構(gòu)造器庆捺,要?jiǎng)?chuàng)造一個(gè)Matcher
的實(shí)例古今,要調(diào)用Pattern
提供的public static方法matcher。matcher接收一個(gè)字符串類型的參數(shù)滔以,表示要匹配的文本捉腥。 - A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.