review20
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的更多相关文章
随机推荐
- 通过less 计算 得出图片均分布局
<style lang="less"> @import "../style/weui.wxss"; // WXSS · 小程序 https://de ...
- JavaScript数据结构与算法-散列练习
散列的实现 // 散列类 - 线性探测法 function HashTable () { this.table = new Array(137); this.values = []; this.sim ...
- MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
忘记密码怎么办? 1.以管理员身份打开cmd2.执行命令tasklist |findstr mysql ,查看正在运行的mysql进程 3.执行命令taskkill /F /PID 13644(此处进 ...
- leetCode 64.Minimum Path Sum (最短路) 解题思路和方法
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- kubernetes 搭建教程
http://blog.csdn.net/u011563903/article/details/71037093
- Python基础-configparser和hashlib模块
configparser模块 import configparser config = configparser.ConfigParser() #将配置写入到文件 config[', 'Compres ...
- 【Network】DDoS攻击防御
DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大,最难防御的攻击之一. 按照发起的方式 ...
- django路由系统之反向生成url
from niubin.service import v1 from django.urls import reverse from django.shortcuts import HttpRespo ...
- Android 结束进程的方法forceStopPackage
ActivityManager sd = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); Method method = Clas ...
- 023_数量类型练习——Hadoop MapReduce手机流量统计
1) 分析业务需求:用户使用手机上网,存在流量的消耗.流量包括两部分:其一是上行流量(发送消息流量),其二是下行流量(接收消息的流量).每种流量在网络传输过程中,有两种形式说明:包的大小,流量的大小. ...