yglodt
yglodt

Reputation: 14551

Regex to replace spring:message tags with th:text equivalent

Using the Eclipse Search function CTRL-H, what would be a regex to replace all occurrences of <spring:message /> tags like:

<label>
    <spring:message code="name" />
</label>
<h1><spring:message code="title" /></h1>

with:

<label th:text="#{name}"></label>
<h1 th:text="#{title}"></h1>

Edit

Nice would be if the regex would also handle replacement of:

<label class="lalala"><spring:message code="name" /></label>

Upvotes: 0

Views: 324

Answers (2)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

You can use this generic one:

<([^>]+)>\s*<\s*spring:message\s+code="([^"]*)"[^<]+<\/\1>

and replace by:

<\1 th:text="#\{\2\}"><\/\1>

Regex Demo

You may have to escape the backslaseh for your IDE like this:

regex:

(?s)<([^>]+)>\\s*<\\s*spring:message\\s+code=\"([^\"]*)\"[^<]+<\\/\\1>

subst

<\\1 th:text=\"#\\{\\2\\}\"><\\/\\1>

Upvotes: 3

daniu
daniu

Reputation: 15008

This should work

(?s)<label>[^<]*<spring:message code="([^"]*)" */>[^<]*</label>

->

<label th:text="#{$1}"></label>

and

<h1><spring:message code="([^"]*)" /></h1>

->

<h1 th:text="#{$1}"></h1>

(The (?s) is there to allow multiline matches: multiline search replace with regexp in eclipse)

Upvotes: 2

Related Questions