[英]Python regex matching pair of quotes or “<>”
I want to find and match #include
directive in C programs and copy them. 我想在C程序中找到并匹配#include
指令并复制它们。
So I want to create a regex, which matches both: 所以我想创建一个匹配两者的正则表达式:
#include <stdlib.h>
#include "mylib.h"
and the copy it for further processing. 并将其复制以供进一步处理。
Sure I could create two simple regex, but I'd like to have only one to match both. 当然,我可以创建两个简单的正则表达式,但我想只有一个匹配两者。 I can't figure out how to make it. 我无法弄清楚如何制作它。
So all I've got is re.compile(r"\\s*#\\s*include\\s*")
所以我所有的都是re.compile(r"\\s*#\\s*include\\s*")
Thank you for advice. 谢谢你的建议。
data = """
#include <stdlib.h>
#include "mylib.h"
"""
import re
pattern = re.compile("#\s*include\s*(?:<.*?>|\".*?\")")
print pattern.findall(data)
Output 产量
['#include <stdlib.h>', '#include "mylib.h"']
以下模式匹配:
#include [<"]\\w+?\\.h[>"]
另一种选择,因为我很少看到使用中的(?(id/name)yes|no)
表达式的任何示例:
#\s*include\s*(?:(<)|("))\w+(\.\w+)?(?(1)>|")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.