using groovy RegEx to grabbing values of attribute: code from grails message tag -


i have parsed gsp file string , want retrieve values of attribute "code" grails message tag (including ${message...) following:

deployment.order.label   no.using.xx.label   default.button.start.label 

i have tried lot matching code="value" | code='value' | code: 'value' | code: "value" in regex, still not working. can regex expert me out? thanks.

this start  <g:message code="deployment.order.label" default="definition order" /> dddfg fgfr ${message(code: "no.using.xx.label", default: 'start')}" usf <g:actionsubmit class="start" id="start"  value="${message(code: 'default.button.start.label', default: 'start')}" action="start"/> kkkkk 
share|improve question
up vote 1 down vote accepted

the regex this:

/(?:code(?:[=:])\s*?\\?(?:["']))([\w+\.]+)*(?:["'])/ig 
  1. (?:code(?:[=:])\s*?\?(?:["'])) - match word code variation of quotes.
  2. ([\w+\.]+)* - match value

  3. (?:["']) - match closed quotes

hope help.

the test script in perl this:

#!/usr/bin/perl use data::dumper;  $subject = "this start  <g:message code=\"deployment.order.label\" default=\"definition order\" /> dddfg fgfr {message(code: \"no.using.xx.label\", default: \'start\')}\" usf <g:actionsubmit class=\"start\" id=\"start\"  value=\"{message(code: \'default.button.start.label\', default: \'start\')}\" action=\"start\"\/> kkkkk";  @matches; push @matches, [$1, $2] while $subject =~ /(?:code(?:[=:])\s*?\\?(?:["']))([\w+\.]+)*(?:["'])/ig;  print dumper(\@matches); 
share|improve answer
    
thanks. take solution. adapt in groovy way: def pattern3=/(?:code(?:[=:])\s*?\\?(?:["']))(\s*[\w+\.]+\s*)*(?‌​:["'])/ def msgs = content.findall(pattern3){match, code->code.trim()} – tommycat apr 6 @ 16:07

only grabbing value of code:

full expression: /(?<=code[=:]\s?["'])([\w.]+)(?=["'])/

(?<=code[=:]\s?["']) using positive lookbehind, assert presence of expression don't include in results

([\w.]+) text

(?=["'])/) using positive lookahead, again assert presence of expression exclude results

def str = '''this start <g:message code="deployment.order.label" default="definition order" /> dddfg fgfr ${message(code: "no.using.xx.label", default: 'start')}" 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] 

Comments