正则表达式matcher.group用法
group是针对括号()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西。
上代码:
@Test
public void test1() {
Pattern pattern = Pattern.compile("页面下载失败\\.url:\\[http://[a-z0-9]+\\.(.+)/.+\\]\\.当前时间戳:\\[([0-9]+)\\]");
Matcher matcher = pattern.matcher("页面下载失败.url:[http://item.jd.com/15626278.html].当前时间戳:[1471415298943]");
if(matcher.find()){
String top_domain = matcher.group(1);
String curr_time = matcher.group(2);
System.out.println(top_domain+"--"+"--"+curr_time);//jd.com----1471415298943
}
} @Test
public void test2(){
String url = "https://item.jd.com/698763154.html";
Pattern pattern = Pattern.compile("https://item.jd.com/([0-9]+).html");
Matcher matcher = pattern.matcher(url);
if(matcher.find()){
System.out.println(matcher.group(1));//
System.out.println(matcher.group(0));//https://item.jd.com/698763154.html
}
} @Test
public void test3(){
String str = "Hello,World! in Java.";
Pattern pattern = Pattern.compile("W(or)(ld!)");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println("Group 0:"+matcher.group(0));//得到第0组——整个匹配
System.out.println("Group 1:"+matcher.group(1));//得到第一组匹配——与(or)匹配的
System.out.println("Group 2:"+matcher.group(2));//得到第二组匹配——与(ld!)匹配的,组也就是子表达式
System.out.println("Start 0:"+matcher.start(0)+" End 0:"+matcher.end(0));//总匹配的索引
System.out.println("Start 1:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一组匹配的索引
System.out.println("Start 2:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二组匹配的索引
System.out.println(str.substring(matcher.start(0),matcher.end(1)));//从总匹配开始索引到第1组匹配的结束索引之间子串——Wor
}
}
总结:其实group(),start(),end()所带的参数i就是正则表达式中的子表达式索引(第几个子表达式)。
正则表达式matcher.group用法的更多相关文章
- 正则表达式matcher.group()用法
本帖转自http://winter8.iteye.com/blog/1463244 group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group( ...
- 优酷电视剧爬虫代码实现一:下载解析视频网站页面(4)补充: Java正则表达式Matcher.group(int group)相关类解析
在Java正则表达式的相关类Matcher中,有如下几个方法: - int groupCount() - String group(int group) - int start(int group) ...
- 初识正则表达式matcher.group
matcher.group中group是匹配()的,group(0)指的是整个串,group(1) 指的是第一个括号里的内容,group(2)指的第二个括号里的内容,以此类推. 例如: str = & ...
- Java正则表达式--Matcher.group函数的用法
原来,group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西. 最近学习正则表达式,发现Java中的一些术语与其 ...
- 【正则表达式】使用正则表达式的group,查找出String中的参数值
需求 指标基本格式: clm.{type}.{hostId}.$metricItem 示例1: // 待匹配表达式:<hostId: 为36位的UUID> summarize(clm.pm ...
- JAVA正则表达式matcher.find()和 matcher.matches()的区别
1.find()方法是部分匹配,是查找输入串中与模式匹配的子串,如果该匹配的串有组还可以使用group()函数.matches()是全部匹配,是将整个输入串与模式匹配,如果要验证一个输入的数据是否为数 ...
- Matcher.group
Exception in thread "main" java.lang.IllegalStateException: No match found at java.util.re ...
- oracle正则表达式regexp_like的用法详解
oracle正则表达式regexp_like的用法详解 /*ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与IN ...
- Solr中Facet用法和Group用法
Group分组划分结果,返回的是分组结果: Facet分组统计,侧重统计,返回的是分组后的数量: 一.Group用法: //组查询基础配置params.set(GroupParams.GROUP, & ...
随机推荐
- AS3中的getChildByName
[转载的...............] 在AS3中,我们可以用getChildByName来获取一个元件,但是要注意返回的类型是DisplayObject,这样一旦我们的元件中有一些自定义的方法就不 ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
- 图论最短路径算法总结(Bellman-Ford + SPFA + DAGSP + Dijkstra + Floyd-Warshall)
这里感谢百度文库,百度百科,维基百科,还有算法导论的作者以及他的小伙伴们...... 最短路是现实生活中很常见的一个问题,之前练习了很多BFS的题目,BFS可以暴力解决很多最短路的问题,但是他有一定的 ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- 标志寄存器在Debug中的表示
在Debug中,标志寄存器是按照有意义的各个标志位单独表示的. 下面列出Debug对我们已知的标志位的表示.
- linux/windows转mac的习惯设置
外接键盘 常用快捷键设置 中英文快捷键名都重新设置一遍 使用ctrl替代command(对 内置 键盘操作) https://jingyan.baidu.com/article/6f2f55a1465 ...
- Python开发——数据类型【元祖】
元祖的定义 tu = (11,22,33,44,) print(tu) # (11, 22, 33, 44) tu = tuple((11,22,33,44,)) print(tu) # (11, 2 ...
- spring jdbc 记录
@Repository("com.example.demo.dao.impl.SmpUserDaoImpl") public class SmpUserDaoImpl implem ...
- 在centos7上使用最简单的方法把php脚本做成服务,随开机启动运行
1.准备文件:coffeetest.service # copy to /usr/lib/systemd/system # systemctl enable coffeetest.service [U ...
- 获取IP地址方法
function getip() { static $ip = ''; $ip = $_SERVER['REMOTE_ADDR']; if(isset($_SERVER['HT ...