简体   繁体   中英

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>

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>

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM