全局匹配模式

-- Start

不区分大小写的匹配模式

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

#!/usr/bin/perl

my $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/perl

my $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/perl

my $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/perl

my $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/perl

my $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/perl

my $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


你可能感兴趣的:(java,String,正则表达式,perl,工具)