2

I'd like to split text at the begining and the end some tags (div and p) not all of them.

Input:
String html = "text<div>some text</div><tag>text</tag><span>asd</span><p>text</p>text";

Output:
text
<div>some text</div>
<tag>text</tag><span>asd</span>
<p>text</p>
text

What regex should i use?

Nyger
  • 215
  • 1
  • 2
  • 7

1 Answers1

1

You could split it with this regex

(?<=</(div|p)>)|(?=<(div|p)>)

But as others recommended use an html parser..


But why use parser..

Consider above regex. It won't work

  • if you have nested tags..(NO REGEX could solve this problem..It's next to impossible)
  • if the tags have attributes
  • if you have arbitrary number of space within tag

Though,its unclear why you want to do this split

Anirudha
  • 32,393
  • 7
  • 68
  • 89