[英]Multiple captures within a string
還沒有找到完全回答這種情況的關於 SO 的 Q/A。 我已經實施了一些解決方案,以達到我所擁有的程度。
我正在解析VCF 文件的 header (metadata) 部分。 每行具有以下格式:
##TAG=<key=val,key=val,...>
我有一個正則表達式,可以解析<>
內的多個 kv 對,但我似乎無法在<>
中添加它並讓它仍然“工作”。
s = 'a=1,b=two,c="three"'
pat = re.compile(r'''(?P<key>\w+)=(?P<value>[^,]*),?''')
match = pat.findall(s)
print(dict(match))
#{'a': '1', 'b': 'two', 'c': '"three"'}
還,
s = 'a=1,b=two,c="three"'
pat = re.compile(r'''(?:(?P<key>\w+)=(?P<value>[^,]*),?)''')
match = pat.findall(s)
print(match)
print(dict(match))
#[('a', '1'), ('b', 'two'), ('c', '"three"')]
#{'a': '1', 'b': 'two', 'c': '"three"'}
所以,我想我可以做到:
s = '<a=1,b=two,c="three">'
pat = re.compile(r'''<(?:(?P<key>\w+)=(?P<value>[^,]*),?)>''')
match = pat.findall(s)
print(match)
print(dict(match))
#[]
#{}
如果可能的話,我真的很想做類似的事情:
\#\#(?P<tag>)=<(?:(?P<key>\w+)=(?P<value>[^,]*),?)>
並捕獲 TAG 和所有 kv 對。 顯然,我希望它“工作”。
我意識到這里的“正確”解決方案可能使用解析器而不是正則表達式。 但我是一個生物信息學的人,而不是一個程序員。 並且格式非常一致,並以(幾乎)始終遵循的標准化規范進行布局。
使用PyPi 正則表達式:
import regex
s = '##TAG=<key=val,key2=val2>'
pat = regex.compile(r'''##(?P<tag>\w+)=<(?:(?P<key>\w+)=(?P<value>[^,<>]*),?)*>''')
match = pat.search(s)
print([match.group("tag"), list(zip(match.captures("key"), match.captures("value")))])
--------------------------------------------------------------------------------
## '##'
--------------------------------------------------------------------------------
(?P<tag> group and capture to \k<tag>:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \k<tag>
--------------------------------------------------------------------------------
=< '=<'
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
(?P<key> group and capture to \k<key>:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1
or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \k<key>
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
(?P<value> group and capture to \k<value>:
--------------------------------------------------------------------------------
[^,<>]* any character except: ',', '<', '>' (0
or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \k<value>
--------------------------------------------------------------------------------
,? ',' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
> '>'
結果: ['TAG', [('key', 'val'), ('key2', 'val2')]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.