博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
全局匹配模式
阅读量:4049 次
发布时间:2019-05-25

本文共 4553 字,大约阅读时间需要 15 分钟。

-- Start

不区分大小写的匹配模式

我们在一节中介绍了如何在EmEditor中进行忽略大小写匹配,下面我们看一下如何在 Perl 和 Java中进行忽略大小写匹配。Perl 使用 /i 修饰符,Java 使用 Pattern.CASE_INSENSITIVE。

#!/usr/bin/perlmy $testText = "I love regular expressions.";if($testText =~ m/REGULAR/i) {	print "finds the word.";} else {	print "cannot find the word.";}
public static void main(String[] args) {		String testText = "I love regular expressions.";				Pattern p = Pattern.compile("REGULAR", Pattern.CASE_INSENSITIVE);		Matcher m = p.matcher(testText);		if (m.find()) {			System.out.println("finds the word.");		} else {			System.out.println("cannot find the word.");		}	}

注释模式

有的正则表达式是非常复杂的,如果没有注释我们是很难看懂的。为了在正则表达式中添加注释,我们必须使用注释模式。Perl 使用 /x 修饰符,Java 使用 Pattern.COMMENTS。

#!/usr/bin/perlmy $testText = "I love regular expressions.";if($testText =~ m/                # 匹配 r, 此处是注释                r                # 匹配 e, 此处是注释                e                # 匹配 g, 此处是注释                g                # 匹配 u, 此处是注释                u                # 匹配 l, 此处是注释                l                # 匹配 a, 此处是注释                a                # 匹配 r, 此处是注释                r/x) {    print "finds the word.";} else {    print "cannot find the word.";}
public static void main(String[] args) {	String testText = "I love regular expressions.";		String regExp = "# 匹配 r \n" +					"r" +					"# 匹配 e \n" +					"e" +					"# 匹配 g \n" +					"g" +					"# 匹配 u \n" +					"u" +					"# 匹配 l \n" +					"l" +					"# 匹配 a \n" +					"a" +					"# 匹配 r \n" +					"r";		Pattern p = Pattern.compile(regExp, Pattern.COMMENTS);	Matcher m = p.matcher(testText);	if (m.find()) {		System.out.println("finds the word.");	} else {		System.out.println("cannot find the word.");	}}

单行模式(single-line mode),也叫点号通配模式(dot-match-all match mode)

我们在中介绍了点号可以匹配任何字符。事实上,这句话并不准确,通常点号不能匹配换行符。为了使点号能够匹配换行符,我们必须使用单行模式,Perl 使用 /s 修饰符,Java 使用 Pattern.DOTALL。

#!/usr/bin/perlmy $testText = "I love reg\nular expressions.";if($testText =~ m/reg.ular/s) {	print "finds the word.";} else {	print "cannot find the word.";}
public static void main(String[] args) {	String testText = "I love reg\nular expressions.";		String regExp = "reg.ular";		Pattern p = Pattern.compile(regExp, Pattern.DOTALL);	Matcher m = p.matcher(testText);	if (m.find()) {		System.out.println("finds the word.");	} else {		System.out.println("cannot find the word.");	}}

多行模式(multiline mode),又称增强的行锚点模式(Enhanced line-anchor match mode)

在程序中,^ 和 $ 用来匹配字符串的开始和结束位置。但是在通常情况下, 它们并不能识别字符串内部的换行符。也就是说用下面的正则表达式无法匹配下面的文本。

表达式:^regular文本:I love \nregular expressions

为了使 ^ 和 $ 能够识别换行符,我们可以使用多行模式,Perl 使用 /m 修饰符,Java 使用 Pattern.MULTILINE。

#!/usr/bin/perlmy $testText = "I love \nregular expressions.";if($testText =~ m/^regular/m) {	print "finds the word.";} else {	print "cannot find the word.";}
public static void main(String[] args) {	String testText = "I love \nregular expressions.";		String regExp = "^regular";		Pattern p = Pattern.compile(regExp, Pattern.MULTILINE);	Matcher m = p.matcher(testText);	if (m.find()) {		System.out.println("finds the word.");	} else {		System.out.println("cannot find the word.");	}}
凡是有一利必有一弊,在多行模式下,如果我就想匹配字符串的开始和结束位置该怎么办呢? 为此, 正则表达式还提供了 \A 和 \Z,它们的作用和普通的 ^ 和 $ 一样,只是在多行模式下,它们的意义不会发生变化,也就是说 \A 和 \Z 永远也不会识别换行符。事实上,不论什么模式,还有一个元字符用来匹配字符串结束位置,那就是 \z,在大部分支持正则表达式的工具中它和 \Z 并没有差别。

综上所述,单行模式和多行模式没有任何关系,但是从名字上看,我们总觉得二者有关系。呵呵,你想多了。

文字文本模式(literal text)

在此模式下,任何字符都代表它本身,事实上等同于不使用正则表达式。要使用此模式,在Perl 中需要在表达式前后加上\Q 和 \E,Java 使用Pattern.LITERAL。

#!/usr/bin/perlmy $testText = "I love regular expressions.";if($testText =~ m/\Qreg.lar\E/) {	print "finds the word.";} else {	print "cannot find the word.";}
public static void main(String[] args) {	String testText = "I love regular expressions.";		String regExp = "reg.lar";		Pattern p = Pattern.compile(regExp, Pattern.LITERAL);	Matcher m = p.matcher(testText);	if (m.find()) {		System.out.println("finds the word.");	} else {		System.out.println("cannot find the word.");	}}

混合使用多种模式

事实上,我们可以混合使用多种模式,看下面的例子。

#!/usr/bin/perlmy $testText = "I love \nregular expressions.";if($testText =~ m/^REGULAR/ism) {	print "finds the word.";} else {	print "cannot find the word.";}
public static void main(String[] args) {	String testText = "I love \nregular expressions.";		String regExp = "^REGULAR";		Pattern p = Pattern.compile(regExp, Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);	Matcher m = p.matcher(testText);	if (m.find()) {		System.out.println("finds the word.");	} else {		System.out.println("cannot find the word.");	}}

--更多参见:

-- 声 明:转载请注明出处
-- Last Updated on 2012-05-12
-- Written by ShangBo on 2012-05-02
-- End

你可能感兴趣的文章
救灾,从来没有胜利
查看>>
.net 2.0中ConfigurationManager替代了原来的ConfigurationSettings
查看>>
Asp.net 2.0中使用Datawindow.net2.0
查看>>
常用命名法:骆驼命名法,匈牙利命名法和帕斯卡命名法
查看>>
Server.MapPath方法测试结果
查看>>
Asp.net 默认配置下,Session莫名丢失的原因及解决办法
查看>>
Datawindow.net中如何使用Calendar控件
查看>>
如何在Datawindow.net中实现让当前行选中,并且当前行以其他颜色显示
查看>>
Datawindow.net如何使用导航栏
查看>>
如何利用Datawindow.net提取Sequence数据
查看>>
小诗,纪念我即将到来的结婚两周年
查看>>
自勉文[出处不详,待考证]
查看>>
中国行政级别
查看>>
国家公务员的级别
查看>>
悼念地震死难者:使整个网页变黑白色(灰色)的特效代码
查看>>
asp.net优化完全技巧
查看>>
道 经
查看>>
德 经
查看>>
藏太甲于桐宫-从电视剧康熙王朝中学到的历史知识
查看>>
开发过程中的沟通问题
查看>>