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具有强大的计算功能,用其统计 ...
随机推荐
- html5、css3及响应式设计入门
一.响应式设计的定义 将三种已有的开发技巧(弹性网格布局.弹性图片.媒体和媒体查询)整合起来,命名为响应式网页设计.真正的响应式设计方法不仅仅只是根据视口大小改变网页布局.相反,它是要从整体上颠覆我们 ...
- java中常用的空判断
Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s));方法二: 比较字 ...
- CentOS下架设Telnet服务器
CentOS下架设Telnet服务器1.什么是Telnet?来自度娘的解释:Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机 ...
- sqlite3编译
1.sqlite3编译: 1.PC版: 1.解压: tar xvf sqlite-autoconf-3140100.tar.gz cd sqlite-autoconf-3140100/ 2.检查配置 ...
- QT移植
QT下载地址:http://download.qt.io/archive/qt/1.编译tslib(touch screen lib) 准备工作:确保以下工具安装完成 sudo apt-get ins ...
- js blob
Blob 是什么? 这里说的是一种JavaScript的对象类型. Oracle 中也有类似的栏位类型. 在 [JS进阶] HTML5 之文件操作(file) 这一篇中用到了File对象,而实际上 f ...
- MyBatis 一级、二级缓存
一级 默认session就有一级缓存,session有C/U/D操作或者session.clearCache();session.commit();都会清空缓存: 二级在mapper.xml中添加&l ...
- android studio导入矢量svg图标技巧
1.在android studio中打开File-->Settings-->Plugins,在Plugins中输入SVG2VectorDrawable搜索插件,并安装. 2.安装完成重启a ...
- Bootstrap入门(七)组件1:字体图标
Bootstrap入门(七)组件1:字体图标 包括200个来自 Glyphicon Halflings 的字体图标,允许 Bootstrap 免费使用. 部分可用图标截图: 所有图标都需要一个基类 ...
- [TPYBoard-Micropython教程之1] 运行第一个脚本——点亮LED
转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 会python就能做硬件! 一.TPYBoard V102开发板 TPYBoard V102 ...