我已将gsp文件解析为String,然后我想从grails消息标记(包括$ {message ...)中检索属性“code”的所有值,如下所示:
deployment.order.label
no.using.xx.label
default.button.start.label
我通过将其与code =“value”|匹配来尝试了很多code ='value'|代码:'value'|代码:RegEx中的“值”,但仍无法正常工作。一些RegEx专家可以帮助我吗?感谢。
this is a start
<g:message code="deployment.order.label" default="Definition order" />
dddfg fgfr
${message(code: "no.using.xx.label", default: 'Start')}"
this is a usf
<g:actionSubmit class="start" id="start"
value="${message(code: 'default.button.start.label', default: 'Start')}" action="start"/>
kkkkk
答案 0 :(得分:1)
正则表达式将是这样的:
/(?:code(?:[=:])\s*?\\?(?:["']))([\w+\.]+)*(?:["'])/ig
([\ w + \。] +)* - 匹配您的值
(?:[&#34;&#39;]) - 匹配封闭引号
希望这会有所帮助。
perl中的测试脚本将是这样的:
#!/usr/bin/perl
use Data::Dumper;
$subject = "this is a start
<g:message code=\"deployment.order.label\" default=\"Definition order\" />
dddfg fgfr
{message(code: \"no.using.xx.label\", default: \'Start\')}\"
this is a usf
<g:actionSubmit class=\"start\" id=\"start\"
value=\"{message(code: \'default.button.start.label\', default: \'Start\')}\" action=\"start\"\/>
kkkkk";
my @matches;
push @matches, [$1, $2] while $subject =~ /(?:code(?:[=:])\s*?\\?(?:["']))([\w+\.]+)*(?:["'])/ig;
print Dumper(\@matches);
答案 1 :(得分:1)
只抓取代码的值:
完整表达:/(?<=code[=:]\s?["'])([\w.]+)(?=["'])/
(?<=code[=:]\s?["'])
使用正面的lookbehind,断言此表达式的存在,但不包含在结果中
([\w.]+)
获取文字
(?=["'])/)
使用正向前瞻,再次声明此表达式的存在,但从结果中排除
def str = '''this is a start <g:message code="deployment.order.label" default="Definition order" />
dddfg fgfr
${message(code: "no.using.xx.label", default: 'Start')}"
this is a usf
<g:actionSubmit class="start" id="start"
value="${message(code: 'default.button.start.label', default: 'Start')}" action="start"/>
kkkkk'''
def group = ( str =~ /(?<=code[=:]\s?["'])([\w.]+)(?=["'])/)
assert 3 == group.count
assert 'deployment.order.label' == group[0][0]
assert 'no.using.xx.label' == group[1][0]
assert 'default.button.start.label' == group[2][0]