Home > Mobile >  Problem with Regex "The string is outside the <li></li>" Python
Problem with Regex "The string is outside the <li></li>" Python

Time:11-07

I need to get string which are not attachments. (Is not between <li></li>)

Example:

i need this string!
<div>i need this string too!</div>
:)<li>I don't need this string!</li>:)

Result:

i need this string!
<div>i need this string too!</div>
:):)

I tried this:

^(?!<li>.*$).*(?<!<\/li>)

But the problem is that if there is text next to my string <li>text</li> For example a smiley:

:)<li>I don't need this string!</li>:)

then the pattern does not work and I don’t understand how to fix it. Can you correct me please?

CodePudding user response:

Use re.sub() to remove the string you don't want, instead of trying to match everything else.

result = re.sub('<li>.*?</li>', '', text);
  • Related