簡體   English   中英

Python正則表達式在兩個字符串之間獲取文本

[英]Python regular Expression to get text between two strings

當我閱讀文本時,在文本的某些行中有類似<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'

Regex101上的演示

如果它是一個漂亮的湯對象,那么它甚至更容易獲得價值。 您將不需要正則表達式。

from bs4 import BeautifulSoup

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

輸出:

General Purpose

組0包含整個比賽; 您需要第1組的內容:

print(d.group(1))

但是通常,使用正則表達式來解析HTML並不是一個好主意(盡管實際上,嵌套的h3標簽應該很少見)。

警告:僅在python中起作用,在pcre或JS中不起作用(JS不支持Lookookhind)。

(?<=\<\h3 class=\"heading\"\>).*?(?=\<\/h3\>)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM