在groovy中使用正则表达式提取子字符串 [英] extract substring using regex in groovy

查看:1640
本文介绍了在groovy中使用正则表达式提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某些文本中包含以下格式:

If I have the following pattern in some text:

def articleContent =  "<![CDATA[ Hellow World ]]>"

我想提取"Hellow World"部分,因此我使用以下代码进行匹配:

I would like to extract the "Hellow World" part, so I use the following code to match it:

def contentRegex = "<![CDATA[ /(.)*/ ]]>"
def contentMatcher = ( articleContent =~ contentRegex )
println contentMatcher[0]

但是,由于正则表达式似乎无法正常工作,我不断收到空指针异常,任何文本和平"的正确正则表达式是什么,以及如何从字符串中收集它?

However I keep getting a null pointer exception because the regex doesn't seem to be working, what would be the correct regex for "any peace of text", and how to collect it from a string?

推荐答案

尝试:

def result = (articleContent =~ /<!\[CDATA\[(.+)]]>/)[ 0 ]​[ 1 ]

但是,我担心您打算使用正则表达式解析xml.如果此cdata是较大的有效xml文档的一部分,则最好使用xml解析器

However I worry that you are planning to parse xml with regular expressions. If this cdata is part of a larger valid xml document, better to use an xml parser

这篇关于在groovy中使用正则表达式提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆