匹配所有出现的常规

时间:2018-03-30 06:13:57

标签: python regex

我想在Python中提取所有出现的模式。 这就是我所做的

import re

string="Any information <p>sent to the server as clear text</p>, may be stolen and used later for <p>identity theft</p> or user impersonation. In addition, several privacy regulations state that sensitive information such as user<p> credentials will always be sent encrypted </p> to the web site."

regex='<p>.*</p>' # obviously it matches starting <p> to the last </p>

if re.findall(regex, String):
    print(re.findall(regex, string))
else:
    print('no match found')

我想提取段落标签的所有内容。我的意思是输出应该是一个看起来像这样的列表

['<p>sent to the server as clear text</p>', '<p>identity theft</p>', '<p> credentials will always be sent encrypted </p>']

我发现很少有类似的问题,但没有达到目的 Find all occurrences of a substring in Python

Finding multiple occurrences of a string within a string in Python

1 个答案:

答案 0 :(得分:0)

像这样更改regex

regex=r"<p>.*?</p>"

它给o / p像:

['<p>sent to the server as clear text</p>', '<p>identity theft</p>', 
 '<p> credentials will always be sent encrypted </p>']