[英]Python Regex - Get strings before and after substring
import re
txt = '<li>one. URL : <a href="http://local.ru">http://local.ru</a> (10.02.2022).</li><li>Two</li><li>Three. URL : <a href="https://local.ru">https://local.ru</a> (15.11.2021).</li>'
re.findall(r'(<li>.*?)\s?URL\s?:\s?(<a.*?>).*?(</a>.*?</li>)', txt)
I need gen output我需要一代 output
[('<li>one.', '<a href="http://local.ru">', '</a> (10.02.2022).</li>'),
('<li>Three.', '<a href="https://local.ru">', '</a> (15.11.2021).</li>')]
If without the first brackets, then it works.如果没有第一个括号,那么它可以工作。 But it does not output the text但它没有 output 的文字
Seems like your regex was too generous on the .*?
似乎您的正则表达式在.*?
, if you limit to non-node with [^<>]
, then you get the expected output. ,如果您使用[^<>]
限制为非节点,那么您将获得预期的 output。
import re
txt = (
'<li>one. URL : <a href="http://local.ru">http://local.ru</a> (10.02.2022).</li>'
'<li>Two</li>'
'<li>Three. URL : <a href="https://local.ru">https://local.ru</a> (15.11.2021).</li>'
)
re.findall(r"(<li>[^<>]*?)\s?URL\s?:\s?(<a[^>]*?>).*?(</a>.*?</li>)", txt)
gives给
[('<li>one.', '<a href="http://local.ru">', '</a> (10.02.2022).</li>'),
('<li>Three.', '<a href="https://local.ru">', '</a> (15.11.2021).</li>')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.