Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试编写一个正则表达式以在解析器中使用。具体来说,我希望能够解析格式如下的字符串:
[SOME-WORD "Quoted string"]
目前,我正在尝试以下表达式:
(?P<capital-item> \[SOME-WORD(\ ?)\" (?P<quoted-string>\w+) \"(\ ?)\])
我正在使用python,然后重新编译以获取扫描仪。编译后,正则表达式与我上面给出的示例字符串不匹配。我在这里搞砸了什么?
>>> import re >>> text = '[SOME-WORD "Quoted string"]' >>> pat = r'\[(?P<capitalitem>SOME-WORD)(\ ?)\"(?P<quotedstring>[\w\s]+)\"(\ ?)\]' >>> re.search(pat, text).groupdict() {'capitalitem': 'SOME-WORD', 'quotedstring': 'Quoted string'}