正則表達式就像一把瑞士軍刀。
"potatoe" ==~ /potatoe/
假如你在groovyConsole運行它,將返回true.這裡有兩件需要注意的事情:
1.==~操作符。它類似于==操作符,但是用於匹配模式而不是計算相等性。
2.正則表達式包含在 '/'中.它會告訴Groovy這是個正則表達式而不是String.
"potatoe" ==~ /potatoe?/
"potato" ==~ /potatoe?/
這裡?表達的意思是'e'是可選的。
"motato" ==~ /potatoe?/
沒有可匹配的。
現在讓我來看些複雜的,我們定義一個方法用來測試正則表達式:
def checkSpelling(spellingAttempt, spellingRegularExpression)
{
if (spellingAttempt ==~ spellingRegularExpression)
{
println("Congratulations, you spelled it correctly.")
} else {
println("Sorry, try again.")
}
}
theRegularExpression = /Wisniewski/
checkSpelling("Wisniewski", theRegularExpression)
checkSpelling("Wisnewski", theRegularExpression)
這裡有兩件事情需要注意:
1.我們定義了函數
def checkSpelling(spellingAttempt, spellingRegularExpression)
函數是一個類似于閉包的代碼集合。函數通常擁有名字,然而閉包可以是匿名的,一旦我們定義了函數,我們就可以在以後重複使用。
在函數中的if語句中,我們判斷 spellingAttempt 參數時候匹配正則表達式通過使用==~操作符
theRegularExpression = /Wis[abcd]niewski/ // requires one of 'a', 'b', 'c' or 'd'
theRegularExpression = /Wis[abcd]?niewski/ // will allow one of 'a', 'b', 'c' or 'd', but not
required (like above)
theRegularExpression = /Wis[a-zA-Z]niewski/ // requires one of any upper\- or lower-case letter
theRegularExpression = /Wis[^abcd]niewski/ // requires one of any character that is '''not''' 'a',
'b', 'c' or 'd'
這裡使用了 *[ ]*.用來分組字符
theRegularExpression = /Wis[abcd]niewski/ // requires one of 'a', 'b', 'c' or 'd'
需要'a', 'b', 'c' , 'd'其中的一個。
theRegularExpression = /Wis[abcd]?niewski/ // will allow one of 'a', 'b', 'c' or 'd', but not
required (like above)
需要'a', 'b', 'c' , 'd'其中的一個,但不是必須的,因為在[ ]後面跟隨了一個?
theRegularExpression = /Wis[a-zA-Z]niewski/ // requires one of any upper\- or lower-case letter
需要一個任意的大寫或小寫字符。
theRegularExpression = /Wis[^abcd]niewski/ // requires one of any character that is '''not''' 'a',
'b', 'c' or 'd'
任何一個不是''a', 'b', 'c' , 'd'
更多的正則表達式操作符可以去參考官方文檔。
分享到:
相关推荐
Groovy 学习笔记 Groovy 学习笔记
Groovy
Java中使用Groovy的三种方式,详细见我的博客。
Groovy 调用 Java 类groovy 调用 Java class 十分方便,只需要在类前导入该 Java 类,在 Groovy 代码中就可以无缝使用该
Groovy学习笔记,内容如下: 1.概念 2.基本语法 3.字符串 4.集合 5.类和闭包 6.控制语句 7.操作符重载 8.I/O操作 9.高级应用 10.正则表达式
对Groovy开发环境设置,基本语法做了简单明了的介绍
Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
概述主要介绍Java、spring与groovy结合使用,高清英文版本
Making Java Groovy
java 动态脚本语言 精通 Groovy
java运行groovy 环境,搭建方便自己练习groovy使用 与后续定时调度动态代码做准备
第1章 数字和字符串 第2章 List的简单介绍 第3章 String的特殊用法 第4章 闭包 第5章 集合+闭包 第6章 Map和Range 第7章 函数
Groovy环境搭建教程中的例子工程,纯Java、纯Groovy以及Java+Groovy混合 教程参考:http://blog.csdn.net/rcom10002/archive/2011/06/26/6568557.aspx
SpringBoot-Gradle-Maven-Java-Groovy:SpringBoot通过不同构建工具和语言实现。
图文并茂,适合入门新手,涵盖了基本的语法讲解及一些DEMO测试,值得一看
NULL 博文链接:https://rsljdkt.iteye.com/blog/859059
Making Java Groovy Kenneth A. Kousen 1.Easier Java 2.Closures, builders, and metaprogramming 3.Gradle for builds, Spock for testing 4.Groovy frameworks like Grails and Griffon 源码
Making Java Groovy--Kenneth.A.Kousen. Java Groovy 很不错的学习资料