Python正则表达式获取两个字符串之间的文本

标签 python regex python-3.x

当我阅读文本时,我有类似 <h3 class="heading">General Purpose</h3> 的字符串在文本的某些行中,现在我只想获得 General Purpose 的值从上面..

d = re.search(re.escape('<h3 class="heading">')+"(.*?)"+re.escape('</h3>'), str(data2))
if d:
    print(d.group(0))

最佳答案

import re

text="""<h3 class="heading">General Purpose</h3>"""
pattern="(<.*?>)(.*)(<.*?>)"

g=re.search(pattern,text)
g.group(2)

输出:

'General Purpose'

Demo on Regex101

如果它是一个漂亮的 soup 对象,那么获取值就更简单了。你不需要正则表达式。

from bs4 import BeautifulSoup

text="""<h3 class="heading">General Purpose</h3>"""
a=BeautifulSoup(text)
print a.select('h3.heading')[0].text

输出:

General Purpose

关于Python正则表达式获取两个字符串之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602714/

相关文章:

python - Numpy 向量化

c# - 正则表达式根据字符串长度匹配不同的子字符串

python - Python 键和值中的嵌套字典

python - 子类化 DecimalField

python - 为使用 Python-C-API 定义的相同自定义类型的函数设置不同的文档字符串

Python docx 属性错误 : 'WindowsPath' object has no attribute 'seek'

python - 从 Python 控制 "daemon-like"linux 脚本

python - 如何防止 turtle 向相反方向移动

Java正则表达式搜索不包括某些字符串的多行文本

regex - 如何使这个 sed 'grab text between 2 keywords' 不区分大小写?