`

Java 正则表达式 (3) -- Quantifiers

阅读更多

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
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

分享到:
评论

相关推荐

    还不会正则表达式?赶快看这篇!.docx

    Quantifiers 用于指定正则表达式的匹配次数。常见的量词有:*、+、?、{n,m} 等。 6. Metacharacters(元字符) Metacharacters 是正则表达式中的一些具有特别含义的字母,例如:.、\、*、+、?、[]、{} 等。这些元...

    一个java正则表达式工具类源代码.zip(内含Regexp.java文件)

    * Summary of regular-expression constructs 正则表达式结构简介: * Construct Matches * Characters 字符: * x The character x x 字符 x * \\ The ...

    PHP和正则表达式教程集合之二第1/2页

    正则表达式快速入门(二) 【导读】在本文里,我们主要介绍子模式(subpatterns),逆向引用(Back references)和量词(quantifiers) 在上篇文章里,我们介绍了正则表达式的模式修正符与元字符,细心的读者也许会发现,...

    熟练使用vim程序编辑器与shell(二).pdf

    * 了解正则表达式的基本语法,如字符_classes、quantifiers、anchors等 * 学会使用正则表达式来匹配和处理文本字符串 * 了解正则表达式的模式匹配和分组捕获 六、项目实践任务 * 熟练掌握vim程序编辑器和shell的...

    quantifiers

    advanced calculus的预备知识其中之一。quantifiers

    chai-quantifiers:chai 的数组量词断言

    安装npm install --save-dev chai chai-quantifiers用法有三个断言可用,适用于数组。 containsAll -- 断言所有数组项对于谓词都为真。 containsOne——断言至少一个数组项对于谓词是真的。 containsExactlyOne -- ...

    1.5- Nested Quantifiers.pdf

    北邮离散数学第一章第五节ppt

    1.4- Predicates and Quantifiers.pdf

    北邮离散数学第一章第四节ppt

    Linq学习100例(含源代码)

    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 ...

    DIRegEx 8.7.1 D4-XE10.1

    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 ...

    DIRegEx 8.7.0 D4-XE10.1

    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 ...

    Thinking in Java 4th Edition

    3 Note on the cover design ....... 4 Acknowledgements ................ 4 Introduction 9 Prerequisites .......................... 9 Learning Java ....................... 10 Goals ........................

    a Practical Theory of Programming 2015-9-19 版

    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 ...

    A Practical Theory of Programming

    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 ...

    linq_standard_query_operators

    Restriction Operators Projection Operators Partitioning Operators Join Operators Concatenation Operators Ordering Operators Grouping Operators ...Quantifiers Aggregate Operators

    Ling学习,用图像方式快速学习Ling

    用图形方式象形介绍Ling技术 简单易懂而且容易记忆 介绍Aggregation Filtering Group Joining Ordering Projection Quantifiers Selection Set Operations 等等......

    [离散数学及其应用(英文第六版)].Discrete.Mathematics.and.its.Applications.djvu

    163 3 The Fundamentals: Algorithms, the Integers, and Matrices....... .167 3 .1 Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....

    PROGRAMMING ACTIONSCRIPT 3.0

    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.rar

    101 LINQ Samples Technologies LINQ Topics Restriction Operators, Aggregate Operators, Conversion Operators... Projection Operators, Quantifiers, Query Execution, Set Operators, Custom Sequence Operators

    Automata Theory and its Applications

    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 ...

Global site tag (gtag.js) - Google Analytics