Home > database >  use regex find html inner text
use regex find html inner text

Time:11-12

------- update

The text block has too much random stuff to use any parsers. Really looking for a regex solution.. for now (QwQ

------- original

Have been spending hours on this. Help please (QAQ

...<div ... class="alert" ...>alertTitle</div>...

I want to be able to find alertTitle after and before </div> from a block of random texts of HTML and other things.

There could be zero or more attributes before and after the class attribute.

I tried regex lookbehind ((?<=alert">)(.)*?(?=<\/div>)) but not really working (T_T

CodePudding user response:

Use this:

<div [^>]*class="alert"[^>]*>(.*?)</div>

You can't use a lookbehind because most regexp engines don't allow variable-length lookbehinds. So use an ordinary match with a capture group around the part that you want.

[^>]* prevents these parts of the match from crossing into another tag.

  • Related