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?

3
  • 7
    A regex called HTML parser. Commented Jul 8, 2013 at 7:46
  • 1
    see also stackoverflow.com/questions/1732348/… Commented Jul 8, 2013 at 7:48
  • Try using an HTML parse like JSoup Commented Jul 8, 2013 at 7:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.