码迷,mamicode.com
首页 > 编程语言 > 详细

从头认识java-11.4 正则表达式(3)-Pattern和Matcher

时间:2015-12-02 22:50:27      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:

这一章节我们来讨论一下Pattern和Matcher。

之前我们都是简单的使用正则表达式来匹配字符串,其实java里面提供了强大的正则匹配类,我们下面将以几个例子来说明。

package com.ray.ch11;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	public static void main(String[] args) {
		String testStr = "abc";
		String[] regulars = { "a", "abc", "//d","a?" };
		System.out.println("input:" + testStr);
		for (String item : regulars) {
			Pattern pattern = Pattern.compile(item);
			Matcher matcher = pattern.matcher(testStr);
			while (matcher.find()) {
				System.out.println("regular:" + item + " start:"
						+ matcher.start() + " end:" + matcher.end());
			}
		}
	}
}

输出:

input:abc
regular:a start:0 end:1
regular:abc start:0 end:3
regular:a? start:0 end:1
regular:a? start:1 end:1
regular:a? start:2 end:2
regular:a? start:3 end:3


从上面的输出可以看到,我们不但可以得到是否匹配的结果,而且通过matcher,我们可以得到很多其他的信息,例如上面的在那个位置匹配,在哪个位置终结。


我们下面改变一下输入,然后再改变一下规则看看另外一个例子:

package com.ray.ch11;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	public static void main(String[] args) {
		String testStr = "java now has regular expression";
		String[] regulars = { "^java", "\\Breg.*", "n.w\\s+h(a|i)s", "s?",
				"s+", "s*", "s{4}", "s{1}", "s{1,3}" };
		System.out.println("input:" + testStr);
		for (String item : regulars) {
			Pattern pattern = Pattern.compile(item);
			Matcher matcher = pattern.matcher(testStr);
			while (matcher.find()) {
				System.out.println("regular:" + item + " start:"
						+ matcher.start() + " end:" + matcher.end());
			}
		}
	}
}

输出:

input:java now has regular expression
regular:^java start:0 end:4
regular:n.w\s+h(a|i)s start:5 end:12
regular:s? start:0 end:0
regular:s? start:1 end:1
regular:s? start:2 end:2
regular:s? start:3 end:3
regular:s? start:4 end:4
regular:s? start:5 end:5
regular:s? start:6 end:6
regular:s? start:7 end:7
regular:s? start:8 end:8
regular:s? start:9 end:9
regular:s? start:10 end:10
regular:s? start:11 end:12
regular:s? start:12 end:12
regular:s? start:13 end:13
regular:s? start:14 end:14
regular:s? start:15 end:15
regular:s? start:16 end:16
regular:s? start:17 end:17
regular:s? start:18 end:18
regular:s? start:19 end:19
regular:s? start:20 end:20
regular:s? start:21 end:21
regular:s? start:22 end:22
regular:s? start:23 end:23
regular:s? start:24 end:24
regular:s? start:25 end:25
regular:s? start:26 end:27
regular:s? start:27 end:28
regular:s? start:28 end:28
regular:s? start:29 end:29
regular:s? start:30 end:30
regular:s? start:31 end:31
regular:s+ start:11 end:12
regular:s+ start:26 end:28
regular:s* start:0 end:0
regular:s* start:1 end:1
regular:s* start:2 end:2
regular:s* start:3 end:3
regular:s* start:4 end:4
regular:s* start:5 end:5
regular:s* start:6 end:6
regular:s* start:7 end:7
regular:s* start:8 end:8
regular:s* start:9 end:9
regular:s* start:10 end:10
regular:s* start:11 end:12
regular:s* start:12 end:12
regular:s* start:13 end:13
regular:s* start:14 end:14
regular:s* start:15 end:15
regular:s* start:16 end:16
regular:s* start:17 end:17
regular:s* start:18 end:18
regular:s* start:19 end:19
regular:s* start:20 end:20
regular:s* start:21 end:21
regular:s* start:22 end:22
regular:s* start:23 end:23
regular:s* start:24 end:24
regular:s* start:25 end:25
regular:s* start:26 end:28
regular:s* start:28 end:28
regular:s* start:29 end:29
regular:s* start:30 end:30
regular:s* start:31 end:31
regular:s{1} start:11 end:12
regular:s{1} start:26 end:27
regular:s{1} start:27 end:28
regular:s{1,3} start:11 end:12
regular:s{1,3} start:26 end:28


解释:

(1)上面的find()函数就像迭代器一样,迭代字符串里面每一个字符。而且find(int)里面还可以放置参数,来决定从第几个位置开始迭代。

(2)上面的start()和end()返回匹配的开始和结束的位置


除了上面的特性,我们还可以在pattern里面加上参数来限定匹配。

package com.ray.ch11;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

	public static void main(String[] args) {
		String testStr = "java now has regular expression\n"
				+ "JAVA now has regular expression";
		String[] regulars = { "^java" };
		System.out.println("input:" + testStr);
		for (String item : regulars) {
			Pattern pattern = Pattern.compile(item, Pattern.CASE_INSENSITIVE
					| Pattern.MULTILINE);
			Matcher matcher = pattern.matcher(testStr);
			while (matcher.find()) {
				System.out.println("regular:" + item + " start:"
						+ matcher.start() + " end:" + matcher.end() + " group:"
						+ matcher.group());
			}
		}
	}
}


输出:

input:java now has regular expression
JAVA now has regular expression
regular:^java start:0 end:4 group:java
regular:^java start:32 end:36 group:JAVA


总结:这一章节主要举例讲述了Pattern和Matcher。


这一章节就到这里,谢谢。

-----------------------------------

目录



从头认识java-11.4 正则表达式(3)-Pattern和Matcher

标签:

原文地址:http://blog.csdn.net/raylee2007/article/details/50148591

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!