I need to replace all characters with - / to - \ between <tagname> ... </tagname> .

The file has at least 10 such tag blocks and I only need to replace the / character within these tags.

How can i do this?

I tried using JAXB, but again, dancing with a tambourine ... Only the regular season remains! But there, too, "damn leg breaks."

After all, somehow the same browser processes the text between the tags <code> ... </code> , and highlights, let's say all ( ) brackets are green and black brackets remain throughout the html body.

2 answers 2

 public static void main(String[] args) { String startTag = "<tagname>"; String endTag = "</tagname>"; String s = "fir/st exa/mple li/ne " + startTag + "fir/st exa/mple li/ne" + endTag + "\nsec/ond exa/mple li/ne " + startTag + "sec/ond exa/mple li/ne" + endTag; System.out.println("До изменения: \n" + s); Pattern pattern = Pattern.compile(startTag + "(.*?)" + endTag); Matcher matcher = pattern.matcher(s); while (matcher.find()) { s = s.replace(matcher.group(0), startTag + matcher.group(1).replace("/", "\\") + endTag); } System.out.println("\nПосле изменения: \n" + s); } 

Output to console:

 До изменения: fir/st exa/mple li/ne <tagname>fir/st exa/mple li/ne</tagname> sec/ond exa/mple li/ne <tagname>sec/ond exa/mple li/ne</tagname> После изменения: fir/st exa/mple li/ne <tagname>fir\st exa\mple li\ne</tagname> sec/ond exa/mple li/ne <tagname>sec\ond exa\mple li\ne</tagname> 

As you can see, the characters / were replaced by \ are only between the specified tags.

It may be possible to solve it somehow easier, for example, something like this:

 s = s.replaceAll(startTag + "(.*?)" + endTag, startTag + "$1".replace("/", "\\") + endTag); 

but the fact is that "$1".replace("/", "\\") for some reason does not work.

Well, to save memory, it is advisable to replace String with StringBuffer / StringBuilder .

  • Small, although how to say ... embarrassing. If after the tag is \n , it does not work. Example: "<pre><code>\n" - Jürgen von Markoff
  • @ Jürgen von Markoff, Try to close the tags. - post_zeew
  • it's not about tags. if in a paragraph, at least one \n falls between the tags, then ... write, it's gone! - Jürgen von Markoff

If the condition is really such, and the <tagname> ... </tagname> cannot be nested, then everything is “almost” simple:

 s/^.*<tagname>.*?(\/).*?<\/tagname>.*$//sg 

I don’t know if there is a global replacement operator (function) in Java, as in Perl, then as an operator / function, otherwise in a loop.

Wrote, "almost" for the reason that, probably between the specified tags, there may be other closing tags. Then it’s not at all easy. Example, let there be a substring:

<tagname>Это пример <sub>непростой</sub> строки<img src="http://host.com/pic.gif"></tagname>

By the condition of the question, the line will look like:

<tagname>Это пример <sub>непростой<\sub> строки<img src="http:\\host.com\pic.gif"></tagname>

And this is not at all correct.

In general, you need to build an AST and already "run with replacements" on it. This solves the problem, the validity of the markup is checked in one.