Regular expressions involving AND in python
P粉988025835
P粉988025835 2023-09-14 16:30:41
0
1
639

I've been struggling for a while now trying to get the correct regular expression for the following task:

I want to remove data from table tags in html file using python. My approach to this is to do the following recursively (store the HTML lines between tags as strings):

s = "Required content"

  1. Reassign the string s to the string with everything between the "<...>" removed.

s = re.sub('<{1}( is not '<' 也不是 '>').*>{1}', '', s)

  1. Repeat this until you are left with s = "what you want".

My question is how to implement the bold part in brackets. Thanks. Your text

I tried

import re

test_str = '<td style="color:blue">Hello</td>'
test_str = re.sub('<{1}^[<>].*>{1}','',test_str)
print(test_str)

You can see that my test string remains the same. What did i do wrong?

The above code I expect gives me test_str = "Hello", I'll feed that back into this method, which then extracts the "", giving me "Hello".

P粉988025835
P粉988025835

reply all(1)
P粉348088995

To negate a character class, place ^ after [. Additionally, you do not need to specify {1} for characters that occur once.

test_str = re.sub('<[^<>]*>', '', test_str)

However, please note that it is more appropriate to use a dedicated HTML parser like BeautifulSoup instead of regular expressions to get data from HTML.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template