Jun Jun - 1 year ago 74
Java Question

Regex capturing within a group

I want to be able to delete all instances of newlines within p tags. But not the ones outside. Example:

<p dir="ltr">Test<br>\nA\naa</p>\n<p dir="ltr">Bbb</p>

This is the regex I came up with


and I replace with


I was hoping that this would work but
seem to be causing issues. Is there any way to do repeated matches like this with a capturing group?

Thanks in advance!

Answer Source

You can use this regex


Regex Demo

With a bit of hack, I was able to do it in Java

String line = "<p dir=\"ltr\">Test<br>\nA\naa</p>\nabcd\n<p dir=\"ltr\">Bbb</p>"; 
System.out.println(line.replaceAll("(?s)((?:<p|\\G(?!\\A))(?:(?!<\\/p>).)*?)[\\n]+", "$1"));

Ideone Demo