JAVA正则表达式之贪婪、勉强和侵占
在JAVA正则表达式中量词(quantifiers)允许指定匹配出现的次数,方便起见,当前 Pattern API 规范下,描述了贪婪、勉强和侵占三种量词。首先粗略地看一下,量词X?、X??和X?+都允许匹配 X 零次或一次,精确地做同样的事情,但它们之间有着细微的不同之处。
| 量 词 种 类 | 意 义 | ||
| 贪婪 | 勉强 | 侵占 | |
X? |
X?? |
X?+ |
匹配 X 零次或一次 |
X* |
X*? |
X*+ |
匹配 X 零次或多次 |
X+ |
X+? |
X++ |
匹配 X 一次或多次 |
X{n} |
X{n}? |
X{n}+ |
匹配 X n 次 |
X{n,} |
X{n,}? |
X{n,}+ |
匹配 X 至少 n 次 |
X{n,m} |
X{n,m}? |
X{n,m}+ |
匹配 X 至少 n 次,但不多于 m 次 |
开始之前准备一段可以重复测试的代码:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("\nRegex:");
Pattern pattern = Pattern.compile(sc.nextLine());
System.out.print("String to search: ");
Matcher matcher = pattern.matcher(sc.nextLine());
boolean found = false;
while (matcher.find()) {
System.out.println("Found the text \"" + matcher.group()
+ "\" starting at index " + matcher.start()
+ " and ending at index " + matcher.end() + ".");
found = true;
}
if (!found) {
System.out.println("No match found.");
}
}
}
}
从贪婪量词开始,构建三个不同的正则表达式:字母a后面跟着?、*和+。接下来看一下,用这些表达式来测试输入的字符串是空字符串时会发生些什么:
Regex:a? String to search: Found the text "" starting at index 0 and ending at index 0. Regex:a*String to search: Found the text "" starting at index 0 and ending at index 0. Regex:a+String to search: No match found.
- 零长度匹配
在上面的例子中,开始的两个匹配是成功的,这是因为表达式a?和a*都允许字符出现零次。就目前而言,这个例子不像其他的,也许你注意到了开始和结束的索引都是 0。输入的空字符串没有长度,因此该测试简单地在索引 0 上匹配什么都没有,诸如此类的匹配称之为零长度匹配(zero-length-matches)。零长度匹配会出现在以下几种情况:输入空的字符串、在输入字符串的开始处、在输入字符串最后字符的后面,或者是输入字符串中任意两个字符之间。由于它们开始和结束的位置有着相同的索引,因此零长度匹配是容易被发现的。
我们来看一下关于零长度匹配更多的例子。把输入的字符串改为单个字符“a”,你会注意到一些有意思的事情:
Regex:a? String to search: a Found the text "a" starting at index 0 and ending at index 1. Found the text "" starting at index 1 and ending at index 1. Regex:a* String to search: a Found the text "a" starting at index 0 and ending at index 1. Found the text "" starting at index 1 and ending at index 1. Regex:a+ String to search: a Found the text "a" starting at index 0 and ending at index 1.
所有的三个量词都是用来寻找字母“a”的,但是前面两个在索引 1 处找到了零长度匹配,也就是说,在输入字符串最后一个字符的后面。回想一下,匹配把字符“a”看作是位于索引 0 和索引 1 之间的单元格中,并且测试用具一直循环下去直到不再有匹配为止。依赖于所使用的量词不同,最后字符后面的索引“什么也没有”的存在可以或者不可以触发一个匹配。
现在把输入的字符串改为一行 5 个“a”时,会得到下面的结果:
Regex:a? String to search: aaaaa Found the text "a" starting at index 0 and ending at index 1. Found the text "a" starting at index 1 and ending at index 2. Found the text "a" starting at index 2 and ending at index 3. Found the text "a" starting at index 3 and ending at index 4. Found the text "a" starting at index 4 and ending at index 5. Found the text "" starting at index 5 and ending at index 5. Regex:a* String to search: aaaaa Found the text "aaaaa" starting at index 0 and ending at index 5. Found the text "" starting at index 5 and ending at index 5. Regex:a+ String to search: aaaaa Found the text "aaaaa" starting at index 0 and ending at index 5.
在“a”出现零次或一次时,表达式a?寻找到所匹配的每一个字符。表达式a*找到了两个单独的匹配:第一次匹配到所有的字母“a”,然后是匹配到最后一个字符后面的索引5。最后,a+匹配了所有出现的字母“a”,忽略了在最后索引处“什么都没有”的存在。
在这里,你也许会感到疑惑,开始的两个量词在遇到除了“a”的字母时会有什么结果。例如,在“”中遇到了字母“b”会发生什么呢?
下面我们来看一下:
Regex:a? String to search: ababaaaab Found the text "a" starting at index 0 and ending at index 1. Found the text "" starting at index 1 and ending at index 1. Found the text "a" starting at index 2 and ending at index 3. Found the text "" starting at index 3 and ending at index 3. Found the text "a" starting at index 4 and ending at index 5. Found the text "a" starting at index 5 and ending at index 6. Found the text "a" starting at index 6 and ending at index 7. Found the text "a" starting at index 7 and ending at index 8. Found the text "" starting at index 8 and ending at index 8. Found the text "" starting at index 9 and ending at index 9. Regex:a* String to search: ababaaaab Found the text "a" starting at index 0 and ending at index 1. Found the text "" starting at index 1 and ending at index 1. Found the text "a" starting at index 2 and ending at index 3. Found the text "" starting at index 3 and ending at index 3. Found the text "aaaa" starting at index 4 and ending at index 8. Found the text "" starting at index 8 and ending at index 8. Found the text "" starting at index 9 and ending at index 9. Regex:a+ String to search: ababaaaab Found the text "a" starting at index 0 and ending at index 1. Found the text "a" starting at index 2 and ending at index 3. Found the text "aaaa" starting at index 4 and ending at index 8.
即使字母“b”在单元格 1、3、8 中出现,但在这些位置上的输出报告了零长度匹配。正则表达式a?不是特意地去寻找字母“b”,它仅仅是去找字母“a”存在或者其中缺少的。如果量词允许匹配“a”零次,任何输入的字符不是“a”时将会作为零长度匹配。在前面的例子中,根据讨论的规则保证了 a 被匹配。
对于要精确地匹配一个模式 n 次时,可以简单地在一对花括号内指定一个数值:
Regex:a{3}
String to search: aa
No match found.
Regex:a{3}
String to search: aaa
Found the text "aaa" starting at index 0 and ending at index 3.
Regex:a{3}
String to search: aaaa
Found the text "aaa" starting at index 0 and ending at index 3.
这里,正则表确定式a{3}在一行中寻找连续出现三次的字母“a”。第一次测试失败的原由在于,输入的字符串没有足够的 a 用来匹配;第二次测试输出的字符串正好包括了三个“a”,触发了一次匹配;第三次测试也触发了一次匹配,这是由于在输出的字符串的开始部分正好有三个“a”。接下来的事情与第一次的匹配是不相关的,如果这个模式将在这一点后继续出现,那它将会触发接下来的匹配:
Regex:a{3}
String to search: aaaaaaaaa
Found the text "aaa" starting at index 0 and ending at index 3.
Found the text "aaa" starting at index 3 and ending at index 6.
Found the text "aaa" starting at index 6 and ending at index 9.
对于需要一个模式出现至少 n 次时,可以在这个数字后面加上一个逗号(,):
Regex:a{3,}
String to search: aaaaaaaaa
Found the text "aaaaaaaaa" starting at index 0 and ending at index 9.
输入一样的字符串,这次测试仅仅找到了一个匹配,这是由于一个中有九个“a”满足了“至少”三个“a”的要求。最后,对于指定出现次数的上限,可以在花括号添加第二个数字。
Regex:a{3,6}
String to search: aaaaaaaaa
Found the text "aaaaaa" starting at index 0 and ending at index 6.
Found the text "aaa" starting at index 6 and ending at index 9.
这里,第一次匹配在 6 个字符的上限时被迫终止了。第二个匹配包含了剩余的三个a(这是匹配所允许最小的字符个数)。如果输入的字符串再少掉一个字母,这时将不会有第二个匹配,之后仅剩余两个 a。
- 捕获组和字符类中的量词
到目前为止,仅仅测试了输入的字符串包括一个字符的量词。实际上,量词仅仅可能附在一个字符后面一次,因此正则表达式abc+的意思就是“a
后面接着 b,再接着一次或者多次的c”,它的意思并不是指abc一次或者多次。然而,量词也可能附在字符类和捕获组的后面,比如,[abc]+表示一次或者多次的
a 或 b 或 c,(abc)+表示一次或者多次的“abc”组。
我们来指定(dog)组在一行中三次进行说明。
Regex:(dog){3}
String to search: dogdogdogdogdogdog
Found the text "dogdogdog" starting at index 0 and ending at index 9.
Found the text "dogdogdog" starting at index 9 and ending at index 18.
上面的第一个例子找到了三个匹配,这是由于量词用在了整个捕获组上。然而,把圆括号去掉,这时的量词{3}现在仅用在了字母“g”上,从而导致这个匹配失败。类似地,也能把量词应用于整个字符类:
Regex:[abc]{3}
String to search: abccabaaaccbbbc
Found the text "abc" starting at index 0 and ending at index 3.
Found the text "cab" starting at index 3 and ending at index 6.
Found the text "aaa" starting at index 6 and ending at index 9.
Found the text "ccb" starting at index 9 and ending at index 12.
Found the text "bbc" starting at index 12 and ending at index 15.
- 贪婪、勉强和侵占量词间的不同
在贪婪、勉强和侵占三个量词间有着细微的不同。
贪婪量词之所以称之为“贪婪的”,这是由于它们强迫匹配器读入(或者称之为吃掉)整个输入的字符串,来优先尝试第一次匹配,如果第一次尝试匹配(对于整个输入的字符串)失败,匹配器会通过回退整个字符串的一个字符再一次进行尝试,不断地进行处理直到找到一个匹配,或者左边没有更多的字符来用于回退了。赖于在表达式中使用的量词,最终它将尝试地靠着1 或 0 个字符的匹配。
但是,勉强量词采用相反的途径:从输入字符串的开始处开始,因此每次勉强地吞噬一个字符来寻找匹配,最终它们会尝试整个输入的字符串。
最后,侵占量词始终是吞掉整个输入的字符串,尝试着一次(仅有一次)匹配。不像贪婪量词那样,侵占量词绝不会回退,即使这样做是允许全部的匹配成功。
为了说明一下,看看输入的字符串是 xfooxxxxxxfoo 时。
Regex:.*foo String to search: xfooxxxxxxfoo Found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13. Regex:.*?foo String to search: xfooxxxxxxfoo Found the text "xfoo" starting at index 0 and ending at index 4. Found the text "xxxxxxfoo" starting at index 4 and ending at index 13. Regex:.*+foo String to search: xfooxxxxxxfoo No match found.
第一个例子使用贪婪量词.*,寻找紧跟着字母“f”“o”“o”的“任何东西”零次或者多次。由于量词是贪婪的,表达式的.*部分第一次“吃掉”整个输入的字符串。在这一点,全部表达式不能成功地进行匹配,这是由于最后三个字母(“f”“o”“o”)已经被消耗掉了。那么匹配器会慢慢地每次回退一个字母,直到返还的“foo”在最右边出现,这时匹配成功并且搜索终止。
然而,第二个例子采用勉强量词,因此通过首次消耗“什么也没有”作为开始。由于“foo”并没有出现在字符串的开始,它被强迫吞掉第一个字母(“x”),在0 和 4 处触发了第一个匹配。测试用具会继续处理,直到输入的字符串耗尽为止。在 4 和 13 找到了另外一个匹配。
第三个例子的量词是侵占,所以在寻找匹配时失败了。在这种情况下,整个输入的字符串被.*+消耗了,什么都没有剩下来满足表达式末尾的“foo”。
你可以在想抓取所有的东西,且决不回退的情况下使用侵占量词,在这种匹配不是立即被发现的情况下,它将会优于等价的贪婪量词。
JAVA正则表达式之贪婪、勉强和侵占的更多相关文章
- JAVA 正则表达式的三种模式: 贪婪, 勉强和占有的讨论
假设待处理的字符串是 xfooxxxxxxfoo 模式.*foo (贪婪模式): 模式分为子模式p1(.*)和子模式p2(foo)两个部分. 其中p1中的量词匹配方式使用默认方式(贪婪型). 匹配开 ...
- java正则表达式语法详解及其使用代码实例
原文地址 译者序(下载代码) 正则表达式善于处理文本,对匹配.搜索和替换等操作都有意想不到的作用.正因如此,正则表达式现在是作为程序员七种基本技能之一*,因此学习和使用它在工作中都能达到很高的效率. ...
- Java正则表达式语法
Java正则表达式 表达式意义: 1.字符 x 字符 x.例如a表示字符a \\ 反斜线字符.在书写时要写为\\\\.(注意:因为java在第一次解析时,把\\\\解析成正则表达式\\,在 ...
- Java正则表达式的总结
Java正则表达式,可以用于很多类型的文本处理, 如匹配,搜索,提取和分析结构化内容. 判断用户的输入是否符合实际需求. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.] ...
- $Java正则表达式基础整理
(一)正则表达式及语法简介 String类使用正则表达式的几个方法: 正则表达式支持的合法字符: 特殊字符: 预定义字符: 方括号表达式: 圆括号表达式:用于将多个表达式组成一个子表达式,可以使用或运 ...
- Java 正则表达式详解
Java 提供了功能强大的正则表达式API,在java.util.regex 包下.本教程介绍如何使用正则表达式API. 正则表达式 一个正则表达式是一个用于文本搜索的文本模式.换句话说,在文本中搜索 ...
- Java正则表达式的语法与示例
Java正则表达式的语法与示例 java 正则表达式 正则表达式语法 java正则表达式语法 java正则表达式 概要: Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 ...
- (转)Java正则表达式的语法与示例
转自:http://www.cnblogs.com/lzq198754/p/5780340.html 概要: Java正则表达式的语法与示例 | |目录 1匹配验证-验证Email是否正确 2在字符串 ...
- java正则表达式学习笔记
Java 正则表达式语法 为了更有效的使用正则表达式,需要了解正则表达式语法.正则表达式语法很复杂,可以写出非常高级的表达式.只有通过大量的练习才能掌握这些语法规则. 本篇文字,我们将通过例子了解正则 ...
随机推荐
- 《Linux内核分析》week1作业-分析一个简单c语言的汇编代码
1.C语言源码 #include <stdio.h> int g(int x){ ; } int f(int x){ return g(x); } int main(){ )+; } 2. ...
- CentOS 6.5上安装Python 2.7.9
CentOS 6.6自带的是Python 2.6.6,而编译llvm需要Python 2.7以上. checking for python... /usr/bin/python checking fo ...
- 3D 灯光介绍
光特性 参考OpenGL的光照模型,把光分成4种独立的成分: 环境光 散射光 镜面光 发散光 环境光:ambient light 环境光是那些在环境中进行了充分的散射,无法分辨其方向的光.它会均匀的照 ...
- 解读 《2014 最流行编程语言》 by Code Eval
此文已转至http://cn.abnerchou.me/2014/02/23/fa87ae80/ 原文:点我 原文翻译:点我 首先了解下CodeEval是做什么的: A tool for auto e ...
- grunt serve Warning: Running "sass:server" (sass) task
使用grunt serve运行时遇到一问题: y@y:ydkt$ grunt serve Running "serve" task Running "clean:serv ...
- LINQ 学习笔记(1)
学习资源参考 : http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 常用方法是 Where, OrderBy, Selec ...
- 用MySQL创建数据库和数据库表
1.使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; +----------+ | Database | +----------+ | mysql ...
- MCS-51单片机内部结构
8051是MCS-51系列单片机的典型产品,我们以这一代表性的机型进行系统的讲解. 8051单片机包含中央处理器.程序存储器(ROM).数据存储器(RAM).定时/计数器.并行接口.串行接口和中断系统 ...
- C51 库函数
C-51软件包的库包含标准的应用程序,每个函数都在相应的头文件(.h)中有原型声明.如果使用库函数,必须在源程序中用预编译指令定义与该函数相关的头文件(包含了该函数的原型声明).例如:#include ...
- 普通的101键盘在Mac上的键位对应
为了方便,搞了一个普通的101有线全键盘 + Magic TrackPad配Macbook. 然后发现了一个小问题,按键对应似乎不像我想的那么完美,F1~F12和Macbook不对应,于 ...