【问题标题】:Regex to replace spring:message tags with th:text equivalent正则表达式用 th:text 等效替换 spring:message 标签
【发布时间】:2018-03-15 11:52:12
【问题描述】:

使用 Eclipse 搜索功能 CTRL-H,用正则表达式替换所有出现的 <spring:message /> 标记,例如:

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

与:

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

编辑

如果正则表达式也可以处理以下内容的替换,那就太好了:

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

【问题讨论】:

    标签: java regex eclipse thymeleaf


    【解决方案1】:

    你可以使用这个通用的:

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

    并替换为:

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

    Regex Demo

    您可能必须像这样为您的 IDE 转义反斜杠:

    正则表达式:

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

    替代

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

    【讨论】:

    • 通用的有效,非常感谢。一个小细节:你错过了#,它应该是
    【解决方案2】:

    这应该可行

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

    ->

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

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

    ->

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

    ((?s) 允许多行匹配:multiline search replace with regexp in eclipse)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-06
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多