【发布时间】:2018-08-30 20:56:34
【问题描述】:
我正在尝试匹配 Python 风格的单行和多行字符串。 到目前为止,这是我想出的:
public const string PythonString = @"(?<string>('''[^(''')]*''')|(""""""[^("""""")]*"""""")|("".*"")|('.*'))";
例如,当您在三重" 匹配字符串中有一个 " 时,它会失败:
"""
msg = "Nothing in this file is used in w3af. This was a test that was truncated by my personal\
lack of interest in using encryption here, my lack of time and the main reason: I'm lazy ;)\
Also, pyrijndael was only used here, so I removed the dependency, which was a problem for debian."
raise Exception(msg)
"""
这里,字符串中的" 强制正则表达式在第一个三元组-" 之后停止匹配,而不是匹配整个块。
我该如何解决这个问题?
【问题讨论】:
-
您能否展示一些示例输入的预期输出?
-
一个常见的误解是,将一个字符序列放入一个否定的 char 类中会导致匹配一个字符序列而不是定义的序列。事实上,
[^(''')]*=[^)(']。所以,你需要@"(?s)(?<string>('''[^']*(?:'(?!'')[^']*)*''')|(""""""[^""]*(?:""(?!"""")[^""]*)*"""""")|(""[^""\\]*(?:\\.[^""\\]*)*"")|('[^'\\]*(?:\\.[^'\\]*)*'))" -
@WiktorStribiżew 谢谢!有用。你能解释一下
(?:'(?!'')[^']*)*的作用吗?