zhengze 四 buhuo 与 not bu huo
来源:互联网 发布:淘宝手机我的店铺 编辑:程序博客网 时间:2024/06/03 20:43
定义:要对多个字符进行重复,用小括号(),来指定要重复的自表达式,然后对这个表达式进行重复。如:(abc)?表示0个或1个abc
1.捕获组:正则表达式中每个“()”内的部分算作一个捕获组,每个捕获组都有一个编号,从1,2,3.....编号0代表整个匹配到的内容。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(<title .*?>)(.*)(</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
System.out.println(m.group(2)+"=="+m.start());
System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
//结果为:<title weight='120px'>nihao</title>==0
<title weight='120px'>==0
nihao==0
</title>==0
2.非捕获组:如果小括号中以?开头,那么这个分组就不会捕获文本,也不会有组编号,因此也不存在回溯引用。
(?:x) 匹配x,但不捕获匹配结果。
(?=x) 零宽度正预测先行断言,但不捕获匹配结果。
(?!x) 零宽度负预测先行断言,但不捕获匹配结果。
(?<=x) 零宽度正预测后发断言,但不捕获匹配结果。
(?<!x)零宽度负预测后发断言,但不捕获匹配结果。
(?:x)与后面4个非捕获组的区别在:
(?:x) :匹配x,group(0)匹配结果包括x
后面4个:匹配x(或不匹配x)位置之前(或之后)的内容,group(0)时匹配的不包括x;
(?:x)与(x)在于只匹配,不捕获结果。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(?:<title .*?>)(.*)(?:</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
System.out.println(m.group(2)+"=="+m.start());
System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
结果:
<title weight='120px'>nihao</title>==0
nihao==0
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
因为不存在编号为2的捕获组。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(?:<title .*?>)(.*)(?:</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
// System.out.println(m.group(2)+"=="+m.start());
//System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
结果:
<title weight='120px'>nihao</title>==0
nihao==0
例子:
1、测试匹配性 (?<!4)56(?=9)这里的含义就是匹配后面的文本56前面不能是4,后面必须是9组成。因此,可以匹配如下文本 5569 ,与4569不匹配。
2 、提取字符串 提取 da12bka3434bdca4343bdca234bm 提取包含在字符a和b之间的数字,但是这个a之前的字符不能是c,b后面的字符必须是d才能提取。
String content="da12bka3434bdca4343bdca234bm";
Pattern p=Pattern.compile("(?<!c)a(\\d+)b(?=d)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());//a3434b,看到非捕获组均为显示结果(?<!c),(?=d)
System.out.println(m.group(1)+"=="+m.start());//3434,0组是整个表达式,捕获组显示的结果:
//System.out.println(m.group(2)+"=="+m.start());
//System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
http://www.jb51.net/article/28035.htm
http://blog.csdn.net/luojinbai/article/details/50082635
1.捕获组:正则表达式中每个“()”内的部分算作一个捕获组,每个捕获组都有一个编号,从1,2,3.....编号0代表整个匹配到的内容。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(<title .*?>)(.*)(</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
System.out.println(m.group(2)+"=="+m.start());
System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
//结果为:<title weight='120px'>nihao</title>==0
<title weight='120px'>==0
nihao==0
</title>==0
2.非捕获组:如果小括号中以?开头,那么这个分组就不会捕获文本,也不会有组编号,因此也不存在回溯引用。
(?:x) 匹配x,但不捕获匹配结果。
(?=x) 零宽度正预测先行断言,但不捕获匹配结果。
(?!x) 零宽度负预测先行断言,但不捕获匹配结果。
(?<=x) 零宽度正预测后发断言,但不捕获匹配结果。
(?<!x)零宽度负预测后发断言,但不捕获匹配结果。
(?:x)与后面4个非捕获组的区别在:
(?:x) :匹配x,group(0)匹配结果包括x
后面4个:匹配x(或不匹配x)位置之前(或之后)的内容,group(0)时匹配的不包括x;
(?:x)与(x)在于只匹配,不捕获结果。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(?:<title .*?>)(.*)(?:</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
System.out.println(m.group(2)+"=="+m.start());
System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
结果:
<title weight='120px'>nihao</title>==0
nihao==0
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
因为不存在编号为2的捕获组。
String content="<title weight='120px'>nihao</title>";
Pattern p=Pattern.compile("(?:<title .*?>)(.*)(?:</title>)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());
System.out.println(m.group(1)+"=="+m.start());
// System.out.println(m.group(2)+"=="+m.start());
//System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
结果:
<title weight='120px'>nihao</title>==0
nihao==0
例子:
1、测试匹配性 (?<!4)56(?=9)这里的含义就是匹配后面的文本56前面不能是4,后面必须是9组成。因此,可以匹配如下文本 5569 ,与4569不匹配。
2 、提取字符串 提取 da12bka3434bdca4343bdca234bm 提取包含在字符a和b之间的数字,但是这个a之前的字符不能是c,b后面的字符必须是d才能提取。
String content="da12bka3434bdca4343bdca234bm";
Pattern p=Pattern.compile("(?<!c)a(\\d+)b(?=d)");
Matcher m=p.matcher(content);
if(m.find()){
System.out.println(m.group(0)+"=="+m.start());//a3434b,看到非捕获组均为显示结果(?<!c),(?=d)
System.out.println(m.group(1)+"=="+m.start());//3434,0组是整个表达式,捕获组显示的结果:
//System.out.println(m.group(2)+"=="+m.start());
//System.out.println(m.group(3)+"=="+m.start());
}
else{
System.out.println("没有匹配到!!!");
}
http://www.jb51.net/article/28035.htm
http://blog.csdn.net/luojinbai/article/details/50082635
阅读全文
0 0
- zhengze 四 buhuo 与 not bu huo
- zhengze......
- bu
- bu
- Huo-deng-M
- the superclass "javax.servlet.http.httpservlet" was not found on the java bu
- 解决:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu
- BU改革
- bu哥
- mn bu
- jsp头文件提示:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu
- Hoj 3132 CHI-HUO Little P
- ZOU YI BU
- hen bu cuo
- Tong bu xinhao
- iphone bu解决方法之一
- HDU 1286 bu
- bu shang zhou de
- 手指静脉图像采集终端的设计和实现
- spring mvc完成json操作
- Python学习笔记(三)—数据类型和变量
- 利用connect by rownum构造日期列表
- java txt导入oracle数据库 date处理
- zhengze 四 buhuo 与 not bu huo
- HTML 表单元素(中)
- svn服务端证书的问题
- Python计算程序运行时间
- SQL Server 2014 虚拟机的自动备份 (Resource Manager)
- Android6.0+ apk下载及安装出现的权限问题
- React+webpack 的开发环境配置步骤(五)
- HDU-3672-Caves
- Linux有问必答:如何检查Linux的内存使用状况