繁体   English   中英

用正则表达式提取子串,总是没有 re.match()

[英]Extract substring with regular expression, always None of re.match()

我想通过正则表达式从字符串中提取一些信息,但结果始终为 None。 源代码如下:

line = '<meta content=\"Allrecipes\" property=\"og:site_name\"/>'
x = re.match(r'property=".+?"',line)
print(x)

我想提取内容和属性元组,我该如何解决?

我会建议更合适的东西。

使用beautifulsoup

from bs4 import BeautifulSoup

line = '<meta content=\"Allrecipes\" property=\"og:site_name\"/>'
soup = BeautifulSoup(line, 'lxml')

print("Content: {}".format(soup.meta["content"]))
print("Property: {}".format(soup.meta["property"]))

输出

Content: Allrecipes
Property: og:site_name

@DirtyBit 的答案比使用正则表达式要好。 但是,如果您仍然想使用正则表达式,它可能会有所帮助( RegexDemo ):

line = '<meta content=\"Allrecipes\" property=\"og:site_name\"/>'
regex = re.search("content=\\\"(?P<content>.*)\\\".*property=\\\"(?P<prop>.*)\\\"\/>",line)
print (regex.groups())

输出:

('Allrecipes', 'og:site_name')

暂无
暂无

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

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