Pattern与Matcher类

模式匹配就是检索和指定模式匹配的字符串。java提供了专门用来进行模式匹配的Pattern类和Matcher类,这些类在java.util.regex包中。

模式对象是对正则表达式的封装。

如:

Pattern p;
String regex = "(http://|www)\56?\\w+\56{1}\\w+\56{1}\\p{Alpha}+";
p = Pattern.compile(regex);

模式对象pattern调用matcher(CharSequence input)方法返回一个Matcher对象matcher,成为匹配对象,参数input用于给出matcher要检索的字符串。

Matcher matcher = pattern.matcher(input);

Matcher对象matcher可以使用下列方法寻找字符串input中是否有和模式regex匹配的子序列(regex是创建模式对象pattern时使用的正则表达式)。

public boolean find():寻找input和regex匹配的下一子序列,如果成功该方法返回true,否则返回false.

public boolean matches():matcher调用该方法判断input是否完全和regex匹配。

public boolean lookingAt():matcher调用该方法判断从input的开始位置是否有和regex匹配的子序列。若lookingAt()方法返回true,matcher调用start()方法和end方法可以得到lookingAt()方法找到的匹配模式的子序列在input中的开始位置和结束位置。matcher调用group()方法可以返回lookingAt()方法找到的匹配模式的子序列。

public boolean find(int start):matcher调用该方法判断input从参数start指定位置开始是否有和regex匹配的子序列,参数start为0,该方法和lookingAt()的功能相同。

public String replaceAll(String replacement):matcher调用该方法返回将与regex匹配的字符串被参数replacement替换后的字符串。

public String replaceFirst(String replacement):matcher调用该方法可以返回一个字符串,该字符串是通过把input中第1个与模式regex匹配的字符串替换为参数replacement指定的字符串得到的。

上述部分方法的使用情况如下所示:

import java.util.regex.Matcher;
import java.util.regex.Pattern; public class Test10 { public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p;
Matcher m;
String regex = "(http://|www)\56?\\w+\56{1}\\w+\56{1}\\p{Alpha}+";
p = Pattern.compile(regex);
String s = "新浪:www.sina.cn,央视:http://www.cctv.com,西农:www.nwsuaf.edu.cn,无:www.edu.cn,百度:www.baidu.com";
System.out.println("原字符串是:" + s);
m = p.matcher(s);
while(m.find())
{
String str = m.group();
System.out.println(str);
}
System.out.println("剔除字符串中的网站网址后得到的字符串:");
String result = m.replaceAll("");
System.out.println(result); } }

运行结果如下所示:

review20的更多相关文章

随机推荐

  1. influxDB概念

    一.基本概念 1)database--数据库,这个同传统数据库的数据库概念. 2)measurement--数据表,在InfluxDB中,measurement即为表的作用,同传统数据库中的table ...

  2. 洛谷 P3263 [JLOI2015]有意义的字符串

    洛谷 首先,看到\((\frac{(b+\sqrt{d})}{2})^n\),很快能够想到一元二次方程的解\(\frac{-b\pm\sqrt{\Delta}}{2a}\). 所以可以推出,\(\fr ...

  3. samba了解

    1. samba是一个网络服务器,用于Linux和Windows之间共享文件 2,amba服务的启动.停止.重启    service smb start|stop|restart3. 掌握samba ...

  4. nodejs post请求

    const http = require('http'); const querystring = require('querystring'); const postData = querystri ...

  5. linux增加 路由使两个不同的网段可以访问

    举例:在交换机上有2个vlan 地址分别是192.168.10.1/24 192.168.20.1/24 2台server:一台A:server地址是192.168.10.3/24,一台B:serve ...

  6. [转载]在服务器端判断request来自Ajax请求(异步)还是传统请求(同步),x-requested-with XMLHttpRequest

    在服务器端判断request来自Ajax请求(异步)还是传统请求(同步) 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步):  两种请求在请求的Header不同,Ajax 异步 ...

  7. Linxu下jenkins部署和基本配置

    一.OpenJdk1.8安装(tomcat  和 jenkins都依赖与java) ubuntu apt-cache search openjdk       #使用apt-cache搜索可以直接使用 ...

  8. iOS UIWebView键盘处理

    让UIWebView弹出键盘上的按钮显示中文    http://www.cr173.com/html/19440_1.html 如果你有下面的问题,此文也许会帮到你. 键盘遮盖了UIWebView. ...

  9. dict字典常用方法总结,数据解构(解包)

    dict {'name':'holle'}字典存储大量关联型数据,可迭代的,最多只有200个键.查询数据速度非常快,符合二分查找(有100个数比如找75会先找到50然后判断,所以2^7次方7次即可找到 ...

  10. 美图秀秀 web开发图片编辑器

    美图秀秀web开发平台 http://open.web.meitu.com/wiki/ 1.环境配置 1.1.设置crossdomain.xml 下载crossdomain.xml文件,把解压出来的c ...