1、Java 正则表达式中的Quantifiers(量词)使用来指定匹配字符出现的次数的,java api中有三种Quantifiers: greedy, reluctant, and possessive。虽然三种quantifiers的作用很相似(见下表),但是三者还是有区别的。(摘自
java.sun.com)
Quantifiers | Meaning |
Greedy |
Reluctant |
Possessive |
X? |
X?? |
X?+ |
X , once or not at all |
X* |
X*? |
X*+ |
X , zero or more times |
X+ |
X+? |
X++ |
X , one or more times |
X{n} |
X{n}? |
X{n}+ |
X , exactly n times |
X{n,} |
X{n,}? |
X{n,}+ |
X , at least n times |
X{n,m} |
X{n,m}? |
X{n,m}+ |
X , at least n but not
more than m times |
2、几个例子(摘自
java.sun.com)
![](/Images/OutliningIndicators/ContractedBlock.gif)
greedy quantifiers
<!---->Enter your regex: a?
Enter input string to search:
I found the text "" starting at index 0 and ending at index 0.
Enter your regex: a*
Enter input string to search:
I found the text "" starting at index 0 and ending at index 0.
Enter your regex: a+
Enter input string to search:
No match found.![](/Images/OutliningIndicators/ContractedBlock.gif)
Zero-Length Matches (1)
<!---->Enter your regex: a?
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
Enter your regex: a*
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
Enter your regex: a+
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.![](/Images/OutliningIndicators/ContractedBlock.gif)
Zero-Length Matches (2)
<!---->Enter your regex: a?
Enter input string to search: aaaaa
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 1 and ending at index 2.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "a" starting at index 3 and ending at index 4.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "" starting at index 5 and ending at index 5.
Enter your regex: a*
Enter input string to search: aaaaa
I found the text "aaaaa" starting at index 0 and ending at index 5.
I found the text "" starting at index 5 and ending at index 5.
Enter your regex: a+
Enter input string to search: aaaaa
I found the text "aaaaa" starting at index 0 and ending at index 5.![](/Images/OutliningIndicators/ContractedBlock.gif)
Zero-Length Matches (3)
<!---->Enter your regex: a?
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "a" starting at index 5 and ending at index 6.
I found the text "a" starting at index 6 and ending at index 7.
I found the text "a" starting at index 7 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.
Enter your regex: a*
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.
Enter your regex: a+
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.![](/Images/OutliningIndicators/ContractedBlock.gif)
exactly n number of times
<!---->Enter your regex: a{3}
Enter input string to search: aa
No match found.
Enter your regex: a{3}
Enter input string to search: aaa
I found the text "aaa" starting at index 0 and ending at index 3.
Enter your regex: a{3}
Enter input string to search: aaaa
I found the text "aaa" starting at index 0 and ending at index 3.![](/Images/OutliningIndicators/ContractedBlock.gif)
at least n times
<!---->Enter your regex: a{3,}
Enter input string to search: aaaaaaaaa
I found the text "aaaaaaaaa" starting at index 0 and ending at index 9.![](/Images/OutliningIndicators/ContractedBlock.gif)
an upper limit
<!---->Enter your regex: a{3,6} // find at least 3 (but no more than 6) a's in a row
Enter input string to search: aaaaaaaaa
I found the text "aaaaaa" starting at index 0 and ending at index 6.
I found the text "aaa" starting at index 6 and ending at index 9.![](/Images/OutliningIndicators/ContractedBlock.gif)
Capturing Groups with Quantifiers
<!---->Enter your regex: (dog){3}
Enter input string to search: dogdogdogdogdogdog
I found the text "dogdogdog" starting at index 0 and ending at index 9.
I found the text "dogdogdog" starting at index 9 and ending at index 18.
Enter your regex: dog{3}
Enter input string to search: dogdogdogdogdogdog
No match found.![](/Images/OutliningIndicators/ContractedBlock.gif)
Character Class with Quantifiers
<!---->Enter your regex: [abc]{3}
Enter input string to search: abccabaaaccbbbc
I found the text "abc" starting at index 0 and ending at index 3.
I found the text "cab" starting at index 3 and ending at index 6.
I found the text "aaa" starting at index 6 and ending at index 9.
I found the text "ccb" starting at index 9 and ending at index 12.
I found the text "bbc" starting at index 12 and ending at index 15.
Enter your regex: abc{3}
Enter input string to search: abccabaaaccbbbc
No match found.
分享到:
相关推荐
Quantifiers 用于指定正则表达式的匹配次数。常见的量词有:*、+、?、{n,m} 等。 6. Metacharacters(元字符) Metacharacters 是正则表达式中的一些具有特别含义的字母,例如:.、\、*、+、?、[]、{} 等。这些元...
* Summary of regular-expression constructs 正则表达式结构简介: * Construct Matches * Characters 字符: * x The character x x 字符 x * \\ The ...
正则表达式快速入门(二) 【导读】在本文里,我们主要介绍子模式(subpatterns),逆向引用(Back references)和量词(quantifiers) 在上篇文章里,我们介绍了正则表达式的模式修正符与元字符,细心的读者也许会发现,...
* 了解正则表达式的基本语法,如字符_classes、quantifiers、anchors等 * 学会使用正则表达式来匹配和处理文本字符串 * 了解正则表达式的模式匹配和分组捕获 六、项目实践任务 * 熟练掌握vim程序编辑器和shell的...
advanced calculus的预备知识其中之一。quantifiers
安装npm install --save-dev chai chai-quantifiers用法有三个断言可用,适用于数组。 containsAll -- 断言所有数组项对于谓词都为真。 containsOne——断言至少一个数组项对于谓词是真的。 containsExactlyOne -- ...
北邮离散数学第一章第五节ppt
北邮离散数学第一章第四节ppt
This sample shows different uses of Quantifiers LINQ - Conversion Operators This sample shows different uses of Conversion Operators LINQ - Miscellaneous Operators This sample shows different ...
greedy and non-greedy repetition quantifiers back references assertions look-behind and look-ahead conditional subpatterns recusive patterns and much more ... DIRegEx also includes two regular ...
greedy and non-greedy repetition quantifiers back references assertions look-behind and look-ahead conditional subpatterns recusive patterns and much more ... DIRegEx also includes two regular ...
3 Note on the cover design ....... 4 Acknowledgements ................ 4 Introduction 9 Prerequisites .......................... 9 Learning Java ....................... 10 Goals ........................
Temporal Logic is yet another formalism that introduces some special operators and quantifiers to describe some aspects of computation. The theory in this book is simpler than any of those just ...
Temporal Logic is yet another formalism that introduces some special operators and quantifiers to describe some aspects of computation. The theory in this book is simpler than any of those just ...
Restriction Operators Projection Operators Partitioning Operators Join Operators Concatenation Operators Ordering Operators Grouping Operators ...Quantifiers Aggregate Operators
用图形方式象形介绍Ling技术 简单易懂而且容易记忆 介绍Aggregation Filtering Group Joining Ordering Projection Quantifiers Selection Set Operations 等等......
163 3 The Fundamentals: Algorithms, the Integers, and Matrices....... .167 3 .1 Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
Quantifiers..295 Alternation...297 Groups297 Flags and properties. 301 Methods for using regular expressions with strings.305 Example: A Wiki parser...306 Chapter 10: Handling events313 Basics of ...
101 LINQ Samples Technologies LINQ Topics Restriction Operators, Aggregate Operators, Conversion Operators... Projection Operators, Quantifiers, Query Execution, Set Operators, Custom Sequence Operators
quantifiers and quantifiers over ideals, and theories of structures related to Cantor's discontinuum. All these were first proved by Rabin in his famous 1969 paper. Our bibliography is relatively ...