正则表达式查找成对的字符串
|
我在http://blog.armbruster-it.de/2010/07/getting-a-list-of-all-i18n-properties-used-in-a-grails-application/中找到了这个很棒的gant脚本/感谢Stefan !
说明:创建在groovy代码和gsp模板中使用的所有i18n属性的列表
def properties = []
new File(\".\").eachFileRecurse {
if (it.file) {
switch (it) {
case ~/.*\\.groovy/:
def matcher = it.text =~ /code:\\s*[\"\'](.*?)[\"\']/
matcher.each { properties << it[1] }
break
case ~/.*\\.gsp/:
def matcher = it.text =~ /code=[\"\'](.*?)[\"\']/
matcher.each { properties << it[1] }
break
}
}
}
println properties.sort().unique().join(\"\\n\")
我试图通过以下方式扩展它。假设我们有soem消息属性,例如:
message(code: \'product.label\', default: \'Product\')
我们想要的脚本输出如下:
product.label=Product
我试图为正则表达式添加一些条件:
def matcher = it.text =~ /code=[\"\'](.*?)[\"\'] | default=\\s*[\"\'](.*?)[\"\']/
并将其填充到属性中。但是由于正则表达式无法找到成对的“代码和默认”部分,因此无法使用。
任何想法如何更改正则表达式或整个脚本来做到这一点?
没有找到相关结果
已邀请:
4 个回复
仇聘发栖
它看起来应该像这样:
物崎巩
贡炮逗握惫
输出格式可以是
结果
碉罕城爸
)。 最好不要遍历文件的内容,而是逐行解析它。这是因为如果一行中有代码,则(第二步)我将查看它是否具有默认代码。不想两次解析整个文件。
这样,我无法在一行中混合使用默认值和没有默认值的消息。我现在可以忍受。 玩得开心!