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
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.
  • ContractedBlock.gif ExpandedBlockStart.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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值