list = [(10) Hello,
Today's weather is cold (43423)
(apple) It's very cold]
There's a string list like this. I want to delete all the strings in here, including parentheses.
I wrote the re module like the code below.
import re
a_list = ['(10) Hello', 'Today's weather is cold(43423)', '(apple) Very cold']
p = re.compile('(.)')
for i in range(len(a_list)):
real_text = re.sub(p, "", a_list[i])
a_list[i] = real_text
print(a_list)
I expected the result to be [Hello, Today's Weather, Very Cold]
The resultant is ["", "", "".
What should I do to make it come out the way I thought?
python regex
Try LAZY QUANTIFIER (*?
import re
a_list = ['(10) Hello', 'Today's weather is cold(43423)', '(apple) Very cold']
p = re.compile('\(.*?\)')
for i in range(len(a_list)):
real_text = re.sub(p, "", a_list[i])
a_list[i] = real_text
print(a_list)
© 2024 OneMinuteCode. All rights reserved.