How to search for ${foo} and replace by <c:out value=“${foo}”/> in Eclipse editor?

不想你离开。 提交于 2019-12-11 12:48:58

问题


Is there a regex to replace the outer part of a string while keeping the inner part?

before:

${person.dog.name}

after:

<c:out value="${person.dog.name}"/>

I need to use it in Eclipse editor, and the inner part changes.


This can be useful too:

Search: \$\{(.*?)\}

Replace:\$\{fn:escapeXml\($1\)\}

${person.dog.name}

become

${fn:escapeXml(person.dog.name)}

回答1:


Find expression with:

(\$\{[^\}]+\})

and replace expression will be:

<c:out value="$1"/>

To find ${person.dog.name} with <c:out value="${person.dog.name}"/> in Eclipse editor!

Edit:

Be careful while using it, because <c:if test="${foo}"> will end up like <c:if test="<c:out value="${foo}" />">




回答2:


Just capture the entire string with ^(.*)$ and then use in the replace function use a back reference like <c:out value="$1"/> where the $1 is the backreference

string.replaceAll("^(.*)$", "<c:out value=\"$1\"/>")

In eclipse see link. You'd put ^(.*)$ into find, <c:out value="$1"/> into replace. select regular expressions, click find and replace.

If you're looking to capture all the ${foo} with <c:out value="${foo}"/>? in some larger text then you'll have to use the regex ([$]{foo}) to find all the desired text then use <c:out value=\"$1\"/> in the replace field.



来源:https://stackoverflow.com/questions/17047789/how-to-search-for-foo-and-replace-by-cout-value-foo-in-eclipse-edito

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!