简体   繁体   English

python 中的正则表达式在标签之间拆分每个文本

[英]Regular expression in python to split each text between tags

Here is my string "<p>First</p><p>Second</p><p>Third</p>" I want to get First, Second & Third into a list.这是我的字符串"<p>First</p><p>Second</p><p>Third</p>"我想将 First、Second 和 Third 放入列表中。 So is there any way to do this in python?那么有没有办法在 python 中做到这一点?

You can use re.findall :您可以使用re.findall

import re

s = "<p>First</p><p>Second</p><p>Third</p>"

re.findall(r"<p>(.*?)</p>", s)
# ['First', 'Second', 'Third']

The parentheses (...) mark a captured group (used by findall if present) and the ?括号(...)标记捕获的组(如果存在,则由 findall 使用)和? matches non-greedily so that you don't get only one match from the first opening to the last closing tag.非贪婪地匹配,这样你就不会从第一个开始到最后一个结束标签只得到一个匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM