繁体   English   中英

字符串中的多个捕获

[英]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")))])

Python 证明| 正则表达式解释

--------------------------------------------------------------------------------
  ##                       '##'
--------------------------------------------------------------------------------
  (?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM