1

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)}
grecof88
  • 87
  • 6
  • Be warned that regex replacements propsed so far will fail hard on constructs like ``, ``, etc. You don't want to wrap that `${}` around with `` or `fn:escapeXml()`. – BalusC Jun 13 '13 at 12:58

2 Answers2

0

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}" />">

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • OP is merely asking how to use it in Eclipse's search&replace, not in Java code (most likely as part of the process to fix completely overseen XSS attack holes in a bunch of JSP files). – BalusC Jun 11 '13 at 16:15
  • Well, fix the answer then? Or just delete if you can't. – BalusC Jun 11 '13 at 16:41
  • How about things like `` in the JSP? This way they would end up like `">` which is syntactically invalid. – BalusC Jun 11 '13 at 17:58
-1

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.

Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
  • OP is asking how to use it in Eclipse's search&replace on JSP files, not in Java code. – BalusC Jun 11 '13 at 16:16
  • Still then, you seem to be expecting that the whole JSP file contains solely an EL expression. Your regex one is namely a "match-all". It does not match individual `${...}` parts which are by itself not already inside a tag such as `` or even e.g. `` (where this replacement is not necessary). – BalusC Jun 11 '13 at 16:39
  • Perhaps I misread the question where it states the input string is `${person.dog.name}` and the resulting string is `` – Ro Yo Mi Jun 11 '13 at 16:46
  • That string is part of JSP file source code. Perhaps you just don't know what a JSP file is? I see that you're famliar with PHP (and probably ASP). Well, a JSP file is like a PHP or ASP file. But a PHP file doesn't contain solely a `echo $foo` nor does an ASP file contain solely a `<%=foo%>`, right? The same way, a JSP file doesn't contain solely a `${foo}`. OP is trying to fix XSS attack holes by putting several instances of it each inside `` (like as you'd need to use `htmlspecialchars()` in PHP). – BalusC Jun 11 '13 at 16:49