[英]How to use regex to reference specific parts?
我有一個Python字符串,其中包含要使用正則表達式提取的信息。
例:
"The weather is 75 degrees with a humidity of 13%"
我只想拉出“ 75”和“ 13”。 到目前為止,這是我在Python中嘗試過的內容。
import re
str = "The weather is 75 degrees with a humidity of 13%"
m = re.search("The weather is \d+ degrees with a humidity of \d+%", str)
matched = m.group()
但是,這顯然匹配整個字符串,而不僅僅是我想要的部分。 如何只提取所需的數字? 我研究過反向引用,但它似乎僅適用於正則表達式模式本身。
m = re.search("The weather is (\d+) degrees with a humidity of (\d+)%", str)
matched = m.groups()
您需要在括號中包裝想要的內容...
>>> s1 = "The weather is 75 degrees with a humidity of 13%"
>>> m = re.search("The weather is (\d+) degrees with a humidity of (\d+)%", s1)
>>> m.groups()
('75', '13')
或者只是使用findall
從任何字符串中獲取數字
>>> re.findall("\d+",s1)
['75', '13']
也許您想使用命名組?
>>> m = re.search("The weather is (?P<temp>\d+) degrees with a humidity of (?P<humidity>\d+)%", s1)
>>> m.group('temp')
'75'
>>> m.group('humidity')
'13'
當您要從文本中提取鍵入的數據(例如數字)時, parse
是一個非常有用的庫。 在許多方面,它與字符串格式相反。 它采用一種模式,並將進行類型轉換。
最簡單的說,它使您避免擔心正則表達式組等。
>>> s = "The weather is 75 degrees with a humidity of 13%"
>>> parse("The weather is {} degrees with a humidity of {}%", s)
<Result ('75', '13') {}>
Result
對象非常易於使用:
>>> r = _
>>> r[0]
'75'
通過指定字段名稱和/或類型轉換,我們可以做得更好。 我們需要做的就是將結果顯示為整數:
>>> parse("The weather is {:d} degrees with a humidity of {:d}%", s)
<Result (75, 13) {}>
如果要使用非索引鍵,請添加字段名稱:
>>> parse("The weather is {temp:d} degrees with a humidity of {humidity:d}%", s)
<Result () {'temp': 75, 'humidity': 13}>
>>> r = _
>>> r['temp']
75
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.