李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
正则表达式--Pattern和Matcher方法(二)
Leefs
2019-12-25 AM
1556℃
0条
# 正则表达式--Pattern和Matcher方法(二) ### 前言 有了上篇文章中对Pattern和Matcher方法的一些基本了解,本篇将讲述《Java编程思想》的第13.6.4小节Pattern和Matcher方法 ### 示例一 下面的类可以用来测试正则表达式,看看它们能否匹配一个输入字符串。 **代码** ```java //Allows you to easily try out regular expressions. //{Args:abcabcabcdefabs "abc+" "(abc)+" "(abc){2,}"} import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestRegularExpression{ public static void main(String[] args){ if(args.length<2){ System.out.println("Usage:\njava TestregularExpression "+"characterSequence regularExpression+"); } System.out.println("Input:\""+args[0]+"\""); for(String arg:args){ System.out.println("RegularExpression:\""+arg+"\""); //compile 编译要匹配的字符串 ,编译要匹配的正则表达式 Pattern p=Pattern.compile(arg); //matcher 匹配compile内的字符串 输入matcher的字符串是否匹配上面的正则表达式 并生成一个matcher对象 Matcher m=p.matcher(args[0]); while(m.find()){//判断是否已经匹配完 //输出匹配字符串和字符串的开始结束位置 System.out.println("Match \""+m.group()+"\" at position "+m.start()+"-"+(m.end()-1)); } } } } ``` 第一个控制台参数是将要用来搜索匹配的输入字符串,后面的一个或多个参数都是正则表达式,它们将被用来在输入的第一个字符串中查找匹配。 Pattern对象表示编译后的正则表达式。测这个例子我们可以看出,我们使用已编译的Pattern上的matcher()方法,加上一个输入字符串,从额共同创造了一个matcher对象,同时,Pattern类还提供了static方法: ```java static boolean matches(String regex,CharSequence input) ``` 该方法用于检查regex是否匹配整个CharSequence类型的input参数。编译后的pattern对象还提供了split()方法,它从匹配了regex的地方分割输入的字符串,返回分割后的子字符串String数组。 通过调用Pattrern.mathder()方法,并传入一个字符串参数,我们得到一个Matcher对象。使用Matcher上的方法,我们能够判断各种不同类型的匹配是否成功: ```java boolean matches(); boolean lookingAt(); boolean find(); boolean find(int start); ``` 其中的matches()方法用来判断整个输入字符串是否匹配正确的正则表达式,而lookingAt()则用来判断该字符串(不必是整个字符串)的开始部分是否能够匹配模式。 ### 示例2 ```java 对字符串Java now has regular expressions验证下列正则表达式是否能够发现一个匹配: ^Java \Breg.* n.w\s+h(a|i)s s? s* s+ s{4} s{1}. s{0,3} ``` **代码** ```java public class StringTest10 { public static void main(String[] args) { String str = "Java now has regular expressions"; for(String arg: args){ Pattern p = Pattern.compile(arg); Matcher m = p.matcher(str); while(m.find()){ System.out.println("Match \"" + m.group() +"\" at positions " + m.start() + "-" + (m.end()-1) ); } System.out.println("\n"); } } } ``` **输出参数** ```java ^Java \Breg.* n.w\s+h(a|i)s s? s* s+ s{4} s{1}. s{0,3} ``` **输出结果** ```java Match "Java" at positions 0-3 Match "now has" at positions 5-11 Match "" at positions 0--1 Match "" at positions 1-0 Match "" at positions 2-1 Match "" at positions 3-2 Match "" at positions 4-3 Match "" at positions 5-4 Match "" at positions 6-5 Match "" at positions 7-6 Match "" at positions 8-7 Match "" at positions 9-8 Match "" at positions 10-9 Match "s" at positions 11-11 Match "" at positions 12-11 Match "" at positions 13-12 Match "" at positions 14-13 Match "" at positions 15-14 Match "" at positions 16-15 Match "" at positions 17-16 Match "" at positions 18-17 Match "" at positions 19-18 Match "" at positions 20-19 Match "" at positions 21-20 Match "" at positions 22-21 Match "" at positions 23-22 Match "" at positions 24-23 Match "" at positions 25-24 Match "s" at positions 26-26 Match "s" at positions 27-27 Match "" at positions 28-27 Match "" at positions 29-28 Match "" at positions 30-29 Match "s" at positions 31-31 Match "" at positions 32-31 Match "s" at positions 11-11 Match "ss" at positions 26-27 Match "s" at positions 31-31 Match "s " at positions 11-12 Match "ss" at positions 26-27 Match "" at positions 0--1 Match "" at positions 1-0 Match "" at positions 2-1 Match "" at positions 3-2 Match "" at positions 4-3 Match "" at positions 5-4 Match "" at positions 6-5 Match "" at positions 7-6 Match "" at positions 8-7 Match "" at positions 9-8 Match "" at positions 10-9 Match "s" at positions 11-11 Match "" at positions 12-11 Match "" at positions 13-12 Match "" at positions 14-13 Match "" at positions 15-14 Match "" at positions 16-15 Match "" at positions 17-16 Match "" at positions 18-17 Match "" at positions 19-18 Match "" at positions 20-19 Match "" at positions 21-20 Match "" at positions 22-21 Match "" at positions 23-22 Match "" at positions 24-23 Match "" at positions 25-24 Match "ss" at positions 26-27 Match "" at positions 28-27 Match "" at positions 29-28 Match "" at positions 30-29 Match "s" at positions 31-31 Match "" at positions 32-31 ``` ### 示例3 **题目** ``` 适用正则表达式(?i)((^[aeiou])|(\\s+[aeiou]))\\w+?[aeiou]\\b匹配字符串Arline ate eight apples and one orange while Anita hadn't any。 ``` **代码** ```java public class StringTest11 { public static void main(String[] args) { Pattern p = Pattern.compile("(?i)((^[aeiou])|(\\s+[aeiou]))\\w+?[aeiou]\\b"); Matcher m = p.matcher("Arline ate eight apples and one orange while Anita hadn't any"); while(m.find()){ System.out.println("Match \""+ m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); } } } ``` **运行结果** ```java Match "Arline" at positions 0-5 Match " ate" at positions 6-9 Match " one" at positions 27-30 Match " orange" at positions 31-37 Match " Anita" at positions 44-49 ``` Matcher.find()方法可用来在CharSequence中查找多个匹配。 模式`\\w+`将字符串划分为单词。find()像迭代器那样前向遍历输入字符串。
标签:
Java
,
Java编程思想
,
字符串
,
正则表达式
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/328.html
上一篇
【转载】正则表达式--Pattern和Matcher
下一篇
【转载】正则表达式--基础部分讲解(一)
取消回复
评论啦~
提交评论
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
43
标签云
Azkaban
线程池
机器学习
Filter
队列
Hadoop
ClickHouse
MySQL
Java编程思想
Beego
微服务
Redis
Ubuntu
DataX
Thymeleaf
RSA加解密
二叉树
CentOS
Spark Core
数据结构和算法
DataWarehouse
Zookeeper
HDFS
Typora
JavaSE
稀疏数组
Netty
Golang
Jenkins
序列化和反序列化
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞