[英]How to match this regular expression in python?
我有以下字符串s =“〜版本11 11 11.1 222 22 22.222”
我想將以下內容提取到以下變量中:
string Variable1 = "11 11 11.1"
string Variable2 = "222 22 22.222"
如何使用正則表達式提取此內容? 還是有更好的替代方法? (請注意,我要提取的令牌之間可能會有可變的間距,並且前導字符可能不是〜,但肯定是一個符號:
例如可能是:
~ VERSION 11 11 11.1 222 22 22.222
$ VERSION 11 11 11.1 222 22 22.222
@ VERSION 11 11 11.1 222 22 22.222
如果正則表達式對此沒有意義,或者有更好的方法,請推薦。 如何在python中將提取預執行為這兩個變量?
嘗試這個:
import re
test_lines = """
~ VERSION 11 11 11.1 222 22 22.222
$ VERSION 11 11 11.1 222 22 22.222
@ VERSION 11 11 11.1 222 22 22.222
"""
version_pattern = re.compile(r"""
[~!@#$%^&*()] # Starting symbol
\s+ # Some amount of whitespace
VERSION # the specific word "VERSION"
\s+ # Some amount of whitespace
(\d+\s+\d+\s+\d+\.\d+) # First capture group
\s+ # Some amount of whitespace
(\d+\s+\d+\s+\d+\.\d+) # Second capture group
""", re.VERBOSE)
lines = test_lines.split('\n')
for line in lines:
m = re.match(version_pattern, line)
if (m):
print (line)
print (m.groups())
給出輸出:
~ VERSION 11 11 11.1 222 22 22.222
('11 11 11.1', '222 22 22.222')
$ VERSION 11 11 11.1 222 22 22.222
('11 11 11.1', '222 22 22.222')
@ VERSION 11 11 11.1 222 22 22.222
('11 11 11.1', '222 22 22.222')
請注意使用帶注釋的詳細正則表達式。
要將提取的版本號轉換為其數字表示形式(即,int,float),請使用@Preet Kukreti的答案中的regexp,並根據建議使用int()
或float()
轉換。
您可以使用String的split方法。
v1 = "~ VERSION 11 11 11.1 222 22 22.222"
res_arr = v1.split(' ') # get ['~', 'VERSION', '11', '11', '11.1', '222', '22', '22.222']
然后根據需要使用元素2-4和5-7。
import re
pattern_string = r"(\d+)\s+(\d+)\s+([\d\.]+)" #is the regex you are probably after
m = re.match(pattern_string, "222 22 22.222")
groups = None
if m:
groups = m.groups()
# groups is ('222', '22', '22.222')
之后,可以根據需要使用int()
和float()
轉換為原始數字類型。 對於高性能代碼,您可能需要預先使用re.compile(...)
預編譯正則表達式,然后在生成的預編譯正則表達式對象上調用match(...)
或search(...)
使用正則表達式絕對容易。 這將是一種方法
>>> st="~ VERSION 11 11 11.1 222 22 22.222 333 33 33.3333"
>>> re.findall(r"(\d+[ ]+\d+[ ]+\d+\.\d+)",st)
['11 11 11.1', '222 22 22.222', '333 33 33.3333']
一旦在列表中獲得結果,就可以索引並獲取各個字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.