简体   繁体   English

Python正则表达式跨行匹配

[英]Python regexp match across lines

I have a bibtex file formatted like this: 我有一个bibtex文件,格式如下:

@inproceedings{baz,
    AUTHOR={{Baz}, {S}. and Bar, {G}. and
      Foo, {M}},
    year={2013}
}

I have managed to capture a single entry (the entire text shown above), but I want a regex in Python that matches everything inside the AUTHOR={} brackets (across the newline). 我设法捕获了一个条目(上面显示的整个文本),但是我想要Python中的正则表达式与AUTHOR={}括号内的所有内容匹配(跨换行符)。 How can I do this in Python? 如何在Python中执行此操作?

re.compile(r"AUTHOR={([\sA-Za-z{},\.]+)},$", re.MULTILINE)

You can use the following regex that checks for 1 level of nested curly braces: 您可以使用以下正则表达式检查1级嵌套花括号:

(?ims)author\s*=\s*[{"]((?:[^{}]+?|{[^}]+?})+?)[}"]

See demo 观看演示

Sample code on IDEONE : IDEONE上的示例代码

import re
p = re.compile(r'(?ims)author\s*=\s*[{"]((?:[^{}]+?|{[^}]+?})+?)[}"]')
test_str = "@inproceedings{baz,\n    AUTHOR = {{Baz}, {S}. and Bar, {G}. and\n      Foo, {M}},\n    year={2013}\n}\n@inproceedings{baz,\n    AUTHOR={{%Baz%}, {S!}. and Bar, {^G^}. and\n      Foo, {<M>}},\n    year={2013}\n}\n"
print [x.group(1) for x in re.finditer(p, test_str)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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