<分区>
如何从字符串中的源代码中获取字符串常量?
例如,这是我要处理的源代码:
var v = "this is string constant + some numbers and \" is also included "
我无法将所有内容都放在引号内。通过使用此正则表达式:"(.*?)"
。
我无法获取 var
、v
、=
或除字符串字符以外的任何其他内容。
<分区>
如何从字符串中的源代码中获取字符串常量?
例如,这是我要处理的源代码:
var v = "this is string constant + some numbers and \" is also included "
我无法将所有内容都放在引号内。通过使用此正则表达式:"(.*?)"
。
我无法获取 var
、v
、=
或除字符串字符以外的任何其他内容。
最佳答案
使用 lookbehind,确保 "前面没有\
import re
data = 'var v = "this is string constant + some numbers and \" is also included "\r\nvar v = "and another \"line\" "'
matches = re.findall( r'= "(.*(?<!\\))"', data, re.I | re.M)
print(matches)
输出:
['this is string constant + some numbers and " is also included ', 'and another "line" ']
关于python - 在 Python 中使用正则表达式从字符串中的源代码中提取字符串常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16001544/