Q:

Split string in java using regex

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?

A:

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

