1

I have this string

"<code>...code1....</code><code><b>kjkjk</b></code>".replaceAll("\\<.*?>","&gt;");

expected output <code>...code1....</code><code>&gt;b&lt;kjkjk &lt;/b&lt;</code>

what i want to do is replace < with &lt; and > with &gt; between all <code> tag using single regular expression. How can i do that ? Is it possible using single regular expression or i will have do separate replaceAll() for < and > ?

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • is it a homework or why do you impose a limitation on the number of `replace` calls? – S. Pauk Mar 30 '15 at 17:56
  • I think multiple `replaceAll` will process the same string again and again – Manish Kumar Mar 30 '15 at 17:58
  • 2
    Do you _need_ to use a regular expression? If not, there are more efficient ways: http://stackoverflow.com/questions/1234510/how-do-i-replace-a-character-in-a-string-in-java/1234531#1234531 – Sean Bright Mar 30 '15 at 18:06

1 Answers1

0

If you want to ensure you encode only the characters between that why don't you first extract it out of and then do myString.replaceAll("\<([^\<\>]*)\>","<" + "$1" + ">"); Otherwise you are going to end up with a very ugly regex that will not be easy to read.

Steve Tomlin
  • 3,391
  • 3
  • 31
  • 63