[英]How can I make same result for re.findall and re.finditer on regular expressions?
I would like to extract words or quotes with quotations without quotations using re.finditer()
function rather than re.findall().我想使用
re.finditer()
function 而不是 re.findall() 来提取带引号的单词或引号。
text = 'This is a very "sweet" and "beautiful" cake.'
-> ['sweet', 'beautiful']
The re.finditer()
function returns with quotations and I have no idea how to get rid of the top and bottom double quotations. re.finditer()
function 返回带引号,我不知道如何摆脱顶部和底部的双引号。
['sweet', 'beautiful']
['"sweet"', '"beautiful"']
import re
text = 'This is a very "sweet" and "beautiful" cake.'
all = re.findall('"(.*?)"', text)
print(all)
all_obj = re.finditer('"(.*?)"', text)
result = []
for obj in all_obj:
result.append(obj.group())
print(result)
Without stripping, is there any solution to fix this issue?如果不剥离,是否有任何解决方案可以解决此问题?
final_result = []
for word in result:
final_result.append(word.rstrip('"').lstrip('"'))
print(final_result)
['sweet', 'beautiful']
Please use .group(1)
to match and extract your group:请使用
.group(1)
匹配并提取您的组:
result.append(obj.group(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.