0

I have a string like the following

~~<b>A<i>C</i></b>~~/~~<u>D</u><b>B</b>~~has done this.

I am trying to get the text inside <b> tag. I am trying

<b>(.+)</b>

But I am getting <b>A<i>C</i></b>~~/~~<u>D</u><b>B</b>, but I need <b>A<i>C</i></b> as first match and <b>B</b> as the second match

Can anyone please help?

4
  • Can you post the regular expression that you've already tried?... Commented Apr 24, 2014 at 15:01
  • Thou shall not try to parse html with regular expressions. Seriously, in most cases you will end up with a pile of unmaintainable and error-prone code - just imagine Nested sections of interest (eG. <b>...<i>...<b>...</b>...</i>...</b>). Make sure that you have a very compelling reason to choose this path. Commented Apr 24, 2014 at 15:09
  • The following questions in the Stack Overflow Regular Expressions FAQ may be of interest: In-depth discussion on the differences between greedy versus non-greedy (listed under "Quantifiers"), and Don't use regex to parse HTML (under "General Information"). Please consider bookmarking the FAQ for future reference. Commented Apr 24, 2014 at 15:12
  • This parsing will be under HTML5 canvas where I have to parse and create canvas text. I am doing this because I can not use html text inside canvas Commented Apr 24, 2014 at 15:21

1 Answer 1

3

You need to use a non-greedy quantifier:

<b>(.+?)</b>

This will ensure that the match stops at the first </b> it finds.

However, I would generally recommend using a proper XML or HTML parser for this sort of thing. Regular expressions are simply not powerful enough to handle the recursive structure of XML.

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

2 Comments

As soon as<b> gets any attributes, this fails.
@JanDvorak true, and that's why I recommend using a proper parser.

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.