提取正则表达式匹配的一部分 [英] Extract part of a regex match
本文介绍了提取正则表达式匹配的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个正则表达式从HTML页面中提取标题。目前我有这样的:
title = re.search('< title>。*< / title>',html ('< title>,'').replace
是否有正则表达式来提取内容,因此我不必删除标记?
谢谢!
解决方案使用
(
)
在regexp和 group(1)
在python中检索捕获的字符串( re.search
将返回没有
如果没有找到结果,所以不直接使用 group()
): title_search = re.search('< title>(。*)< / title>',html,re.IGNOREC ASE)
如果title_search:
title = title_search.group(1)
I want a regular expression to extract the title from a HTML page. Currently I have this:
title = re.search('<title>.*</title>', html, re.IGNORECASE).group()
if title:
title = title.replace('<title>', '').replace('</title>', '')
Is there a regular expression to extract just the contents of so I don't have to remove the tags?
thanks!
解决方案
Use (
)
in regexp and group(1)
in python to retrieve the captured string (re.search
will return None
if it doesn't find the result, so don't use group()
directly):
title_search = re.search('<title>(.*)</title>', html, re.IGNORECASE)
if title_search:
title = title_search.group(1)
这篇关于提取正则表达式匹配的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文