利用Groovy的正则表达式处理字符串
关键字: groovy
常用的正则表达式
匹配意义 表达式 等价符号
数字 \d [0-9]
非数字 \D [^0-9]
word字符 \w [a-zA-Z0-9]
非word字符 \W [^a-zA-Z0-9]
空白字符 \s [\t\n\f\r\v]
非空白字符 \S [^\s]
汉字 [\u4E00-\u9FA5]
非汉字 [^\u4E00-\u9FA5]
学习java已经两年一直都没搞懂java的正则表达式,每次学一点就会发困,感觉非常费劲。但在学习Groovy的过程中,还是很轻松的搞懂了正则表达式的一些皮毛。(惊叹Groovy真是Groovy)
Groovy同样提供了3种方便的运算符
• 匹配操作符 (==~)
• 查找操作符 (=~)
• 模式操作符 (~string)
匹配操作符 (==~)
主要用于检查字符串是否匹配 返回true或false
Java代码
assert "abc" ==~ /abc/
assert "abc" ==~ ".*c\$"
assert "abc" ==~ /abc/
assert "abc" ==~ ".*c\$" 查找操作符 (=~)
经常在复制网页上的网页时,发现复制的代码左边有数字序号,如果代码长的话自己动手去掉前面的序号也很费劲.利用下面的代码可以将所有的数字都去掉
Java代码
def code = """01 import groovy.text.SimpleTemplateEngine
02
03 /**
04 * Simple User Groovy Bean.
05 */
06 class User {
07 String firstName;
08 String lastName;
09 }
10
11 def emailTemplate = this.class.getResource("nightlyReportsEmail.gtpl")
12 def binding = [
13 "user": new User(firstName: "Christopher", lastName: "Judd"),
14 "date": new Date()
15 ]
16 def engine = new SimpleTemplateEngine()
17 def email = engine.createTemplate(emailTemplate).make(binding)
18 def body = email.toString()
19
20 println body"""
def matcher = code =~ /\d/
def newCode = matcher.replaceAll('')
println newCode
def code = """01 import groovy.text.SimpleTemplateEngine
02
03 /**
04 * Simple User Groovy Bean.
05 */
06 class User {
07 String firstName;
08 String lastName;
09 }
10
11 def emailTemplate = this.class.getResource("nightlyReportsEmail.gtpl")
12 def binding = [
13 "user": new User(firstName: "Christopher", lastName: "Judd"),
14 "date": new Date()
15 ]
16 def engine = new SimpleTemplateEngine()
17 def email = engine.createTemplate(emailTemplate).make(binding)
18 def body = email.toString()
19
20 println body"""
def matcher = code =~ /\d/
def newCode = matcher.replaceAll('')
println newCode
以前做项目的时候,由于项目中需要将ocr识别的文字存入数据库,发现由于一些特殊字符导致插入数据库出现异常,以前一直想使用正则表达式进行过滤,就是不得其门而入
Java代码
def sso = /‘’Groovy正则表达式0-9;[]‘;“;';过滤标点符号/
def matcher = sso =~ /['’‘;“”!!;\]\[]/ //可以加入任何需要过滤的特殊字符(注意有的需要转义)
def newS = matcher.replaceAll('')
println newS
//result:Groovy正则表达式0-9过滤标点符号
如果只想留下汉字的话,可以将上面的matcher改为/[^\u4E00-\u9FA5]/
def sso = /‘’Groovy正则表达式0-9;[]‘;“;';过滤标点符号/
def matcher = sso =~ /['’‘;“”!!;\]\[]/ //可以加入任何需要过滤的特殊字符(注意有的需要转义)
def newS = matcher.replaceAll('')
println newS
//result:Groovy正则表达式0-9过滤标点符号
如果只想留下汉字的话,可以将上面的matcher改为/[^\u4E00-\u9FA5]/
模式操作符(~string)
运用该运算符将string定义为pattern(java.util.regex.Pattern),并运用这个pattern可以做到检索一个字符串中出现符合该pattern的string。(语言组织不好,还是看例子清楚点)
Java代码
def saying = """Now is the time for all good men (and women) to come to the aid
of their country"""
def pattern = ~/(\w+en)/
def matcher = pattern.matcher(saying)
def count = matcher.getCount()
println "Matches = ${count}"
for(i in 0..<count) {
println matcher[i]
}
result:
Matches = 2
["men", "men"]
["women", "women"]
关键字: groovy
常用的正则表达式
匹配意义 表达式 等价符号
数字 \d [0-9]
非数字 \D [^0-9]
word字符 \w [a-zA-Z0-9]
非word字符 \W [^a-zA-Z0-9]
空白字符 \s [\t\n\f\r\v]
非空白字符 \S [^\s]
汉字 [\u4E00-\u9FA5]
非汉字 [^\u4E00-\u9FA5]
学习java已经两年一直都没搞懂java的正则表达式,每次学一点就会发困,感觉非常费劲。但在学习Groovy的过程中,还是很轻松的搞懂了正则表达式的一些皮毛。(惊叹Groovy真是Groovy)
Groovy同样提供了3种方便的运算符
• 匹配操作符 (==~)
• 查找操作符 (=~)
• 模式操作符 (~string)
匹配操作符 (==~)
主要用于检查字符串是否匹配 返回true或false
Java代码
assert "abc" ==~ /abc/
assert "abc" ==~ ".*c\$"
assert "abc" ==~ /abc/
assert "abc" ==~ ".*c\$" 查找操作符 (=~)
经常在复制网页上的网页时,发现复制的代码左边有数字序号,如果代码长的话自己动手去掉前面的序号也很费劲.利用下面的代码可以将所有的数字都去掉
Java代码
def code = """01 import groovy.text.SimpleTemplateEngine
02
03 /**
04 * Simple User Groovy Bean.
05 */
06 class User {
07 String firstName;
08 String lastName;
09 }
10
11 def emailTemplate = this.class.getResource("nightlyReportsEmail.gtpl")
12 def binding = [
13 "user": new User(firstName: "Christopher", lastName: "Judd"),
14 "date": new Date()
15 ]
16 def engine = new SimpleTemplateEngine()
17 def email = engine.createTemplate(emailTemplate).make(binding)
18 def body = email.toString()
19
20 println body"""
def matcher = code =~ /\d/
def newCode = matcher.replaceAll('')
println newCode
def code = """01 import groovy.text.SimpleTemplateEngine
02
03 /**
04 * Simple User Groovy Bean.
05 */
06 class User {
07 String firstName;
08 String lastName;
09 }
10
11 def emailTemplate = this.class.getResource("nightlyReportsEmail.gtpl")
12 def binding = [
13 "user": new User(firstName: "Christopher", lastName: "Judd"),
14 "date": new Date()
15 ]
16 def engine = new SimpleTemplateEngine()
17 def email = engine.createTemplate(emailTemplate).make(binding)
18 def body = email.toString()
19
20 println body"""
def matcher = code =~ /\d/
def newCode = matcher.replaceAll('')
println newCode
以前做项目的时候,由于项目中需要将ocr识别的文字存入数据库,发现由于一些特殊字符导致插入数据库出现异常,以前一直想使用正则表达式进行过滤,就是不得其门而入
Java代码
def sso = /‘’Groovy正则表达式0-9;[]‘;“;';过滤标点符号/
def matcher = sso =~ /['’‘;“”!!;\]\[]/ //可以加入任何需要过滤的特殊字符(注意有的需要转义)
def newS = matcher.replaceAll('')
println newS
//result:Groovy正则表达式0-9过滤标点符号
如果只想留下汉字的话,可以将上面的matcher改为/[^\u4E00-\u9FA5]/
def sso = /‘’Groovy正则表达式0-9;[]‘;“;';过滤标点符号/
def matcher = sso =~ /['’‘;“”!!;\]\[]/ //可以加入任何需要过滤的特殊字符(注意有的需要转义)
def newS = matcher.replaceAll('')
println newS
//result:Groovy正则表达式0-9过滤标点符号
如果只想留下汉字的话,可以将上面的matcher改为/[^\u4E00-\u9FA5]/
模式操作符(~string)
运用该运算符将string定义为pattern(java.util.regex.Pattern),并运用这个pattern可以做到检索一个字符串中出现符合该pattern的string。(语言组织不好,还是看例子清楚点)
Java代码
def saying = """Now is the time for all good men (and women) to come to the aid
of their country"""
def pattern = ~/(\w+en)/
def matcher = pattern.matcher(saying)
def count = matcher.getCount()
println "Matches = ${count}"
for(i in 0..<count) {
println matcher[i]
}
result:
Matches = 2
["men", "men"]
["women", "women"]