groovy regex groups(groovy正则表达式组)
先看一个java正则表达式的例子.
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class TestMatch {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("G.*");
Matcher matcher = pattern.matcher("Groovy");
System.out.println(matcher.matches());
}
}
其实这个代码的意思就是Groovy是不是匹配正则表达式G.*
为了一个简单的判断是不是匹配,就写这么多代码。看groovy是如何实现的。
matcher = "Groovy" =~ /G.*/
println(matcher.matches())
2行代码搞定。
正则表达式组(regex groups)又怎么用?再看一个groovy的例子.
text = """
Lorem 1:30 PM ipsum dolor 12:00 PM sit amet, consectetuer adipiscing elit.
"""
HOUR = /10|11|12|[0-9]/
MINUTE = /[0-5][0-9]/
AM_PM = /AM|PM/
time = /($HOUR):($MINUTE) ($AM_PM)/
matcher = text =~ time
println(matcher[0] == ["1:30 PM", "1", "30", "PM"]) //First Match
println(matcher[0][0] == "1:30 PM") //First match group in the first match
println(matcher[0][1] == "1") //Second match group in the first match (HOUR)
println(matcher[0][2] == "30") //Third match group in the first match (MINUTE)
println(matcher[0][3] == "PM") //Fourth match group in the first match (AM_PM)
println(matcher[1] == ["12:00 PM", "12", "00", "PM"]) //Second Match
println(matcher[1][0] == "12:00 PM") //First match group in the second match
println(matcher[1][1] == "12") //Second match group in the second match (HOUR)
println(matcher[1][2] == "00") //Third match group in the second match (MINUTE)
println(matcher[1][3] == "PM") //Fourth match group in the second match (AM_PM)
其中time就可以认为是一个正则表达式组,它有三个“组员”: ($HOUR),($MINUTE),($AM_PM)
注意,用()括起来才表示是一个“组员”,否则每个字符都是一个组员.要注意一个问题matcher.matches()意思是“是否整个字符串都匹配正则表达式”,对于这个例子,虽然text中有2个匹配,但是matcher.matches()的结果是false,因为对于整个字符串来说不匹配.通过这个细节可以体会正则表达式组的作用.
再看一个例子:
matcher = ".groovyd,aa3" =~ /(\.groovy.),(aa.)/
println(matcher.matches())
println(matcher.getCount())
/(\.groovy.),(aa.)/有2个“组员”
再看一个例子:
matcher = ".groovyd,aa3" =~ /\.groovy.,aa./
println(matcher.matches())
/\.groovy.,aa./有12个组员.
参考资料:
1. http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
2. http://groovy.codehaus.org/FAQ+-+RegExp
3. http://developer.51cto.com/art/200906/129179.htm
4. http://docs.oracle.com/javase/7/docs/api/
groovy regex groups(groovy正则表达式组)的更多相关文章
- Error:Cannot compile Groovy files: no Groovy library is defined for module 'xxxx' 错误处理
出现 Error:Cannot compile Groovy files: no Groovy library is defined for module 'xxxx' 只要在 project str ...
- Groovy学习记录-------Groovy安装/配置
1.Groovy SDK下载 Groovy SDK官网下载地址: http://www.groovy-lang.org/download.html 每个版本有五个选项可供下载,依次为: binary ...
- 快学Scala 第十六课 (shell调用,正则表达式,正则表达式组,stripMargin妙用)
shell调用:(管道符前加#号,执行shell用!) import sys.process._ "ls -al" #| "grep x" ! 正则表达式:(r ...
- regEx in Groovy
// 使用正则 得到非纯XML文件中的信息 // Response 经常得到的不是纯XML def pattern = ~/(<NewDataSet>).*(<\/NewDataSe ...
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
Objective-C RegEx Categories https://github.com/bendytree/Objective-C-RegEx-Categories 使用说明:将 RegExC ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [LeetCode] 839. Similar String Groups 相似字符串组
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...
- 推荐一本学习Groovy的书籍Groovy程序设计!
有朋友公司在用groovy开发,于是推荐我学习一下,搜到了这本书: 花了一个月时间读完了这本书!写的很棒,几乎没有废话,全书都是很重要的知识点和很好的讲解,确实像封面说的那样,使用的好可以提高开发效率 ...
- Matlab boxplot for Multiple Groups(多组数据的箱线图)
在画之前首先介绍一下Matlab boxplot,下面这段说明内容来自http://www.plob.org/2012/06/10/2153.html 由于matlab具有强大的计算功能,用其统计 ...
随机推荐
- leetcode--003 LRU cache
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHAAAACmCAIAAAA9PO+sAAAgAElEQVR4nO3du3HbytvH8X8zqoB12A ...
- 【View层】界面绘制
[引用]:http://www.jianshu.com/p/c5fc8c6b967a [View层]IOS纯代码绘制界面(一) 字数2303 阅读385 评论2 喜欢16 IOS开发中界面绘制占据了绝 ...
- linux内核——1.概述
1.结构 linux中,我们把操作系统分为内核空间和用户空间.用户通过用户空间与操作系统打交道.用户要通过系统调用访问内核空间.下图为Linux体系结构,shell应该为在最顶层. 系统调用,下面链接 ...
- oracle关键字(保留字)
其实这个东西可以在oracle 上输入一个sql语句就可以得到: select * from v$reserved_words order by keyword asc; //order 后边不是 ...
- wildfly 如何设置外网访问
wildfly的默认配置是不支持外网访问的, 要想实现外网访问需要修改standalone.xml配置文件. 配置文件所在路径:wildfly/standalone/configuration/sta ...
- 纠错输出编码法ECOC
纠错输出编码法(Error-Correcting Output Codes,ECOC)不仅能够将多类分类问题转化为多个两类问题,而且利用纠错输出码本身具有纠错能力的特性,可以提高监督学习算法的预测精度 ...
- jmeter接口测试实践
一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...
- delphi bitbutton和speedbutton.button有什么区别
http://zhidao.baidu.com/link?url=LT9_iwv47GfSp3m0MmUSablZrPOxzjKtqWW1yVMTbHnIs2DdCE-0qX2bwXZ2020x_Jl ...
- win10新特性,ubuntu子系统(安装及配置)
最新版win10下可以直接跑ubuntu镜像,直接入正题. 这里如果你没有可能是你的版本不是最新的,我这里是最新的win10直接是有这个功能的.勾选后会要求重启,确定即可. 然后win键弹出搜索,输入 ...
- jQuery如何创建元素
1.$("<ul>").attr("id","taglist").appendTo("#tagCloud") ...