It is necessary between the tags to replace e with E and z with Z , for example.

String s= "<pre><code>еееееееееееzzzzzzzzzzzzzz</code></pre>"; String startTag = "<pre><code>"; String endTag = "</code></pre>"; 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("z", "Z").replace("е", "Ё") + endTag); } System.out.println("\nПосле изменения: \n" + s); 

Displays everything correctly:

 <pre><code>ЁЁЁЁЁЁЁЁЁЁЁZZZZZZZZZZZZZZ</code></pre> 

It is necessary to add \ n anywhere in the condition (to the variable s ), as the implementation stops working:

  s= "<pre><code>ееее\nеееееееzzzzzzzzzzzzzz</code></pre>"; 

will issue:

 <pre><code>ееее еееееееzzzzzzzzzzzzzz</code></pre> 

those. - without changes. What am I doing wrong? I will not think.

  • Wrote the answer in the question to which you refer. - ReinRaus

1 answer 1

Use the PCRE_S_DOTALL flag.
The default metacharacter . does not include a line break and only the application of this flag includes a match . with a line break literal.

 Pattern.compile( regex, Pattern.S ); Pattern.compile( regex, Pattern.DOTALL ); 

Both options are equivalent.

  • Respecting, amigo !!! I have been struggling with this issue for more than a week, I have already climbed into a multithread ... and I have a penny to do! In one line! Thank!!! ........................... Maybe someone will come in handy: .................. ..... Pattern pattern = Pattern.compile(startTag + "(.*?)" + endTag, Pattern.DOTALL); - Jürgen von Markoff