[英]To avoid pattern inside quotes using python regex
I am trying to match a multi line string str as follows, 我试图按如下方式匹配多行字符串str,
call fun (TP$NULL,
ERROR_CODE,
PROG_ID || 'Password Incorrect, Please try again') /*END*/
I want to process this string with out missing anythings (ie, spaces, tab & newline) 我想处理此字符串而不会丢失任何内容(即空格,制表符和换行符)
I tried get it by using comma which separate three arguments 我尝试通过使用逗号分隔三个参数来获取它
re.search (r'(\s*call\s+fun\s*\()(.+),(.+),(.+)(\).*)', str, re.DOTALL).groups
('call fun (', 'TP$NULL, ERROR_CODE', " PROG_ID || 'Password Incorrect", " Please try again'", ') /*END*/')
see the comma inside quotes in the third argument creates problem. 请参阅第三个参数中引号内的逗号会引起问题。
When I am trying the regex below, 当我尝试以下正则表达式时,
re.search (r'call fun \((.+),(.+), (.*\'.*,.*\'.*)\)' , str, re.DOTALL).groups()
above case is working but below string str2 is not working. 大写的情况下工作,但下面的字符串str2不起作用。
call fun (TP$NULL, err, PROG_D)
I tried negative look ahead as follows, but no luck, 我尝试了以下负面观察,但没有运气,
re.search (r'call fun \((.+),(.+),(?!\'.+\')(.+)\)' , str, re.DOTALL).groups()
you likely just want the first 2 groups to not be greedy, eg using your previous to last version: 您可能只希望前两个组不要贪心,例如使用以前的版本到最新的版本:
>>> for g in re.search (r'call fun \((.+?),(.+?),(.*)\)', str2, re.DOTALL).groups():
... print(g)
...
TP$NULL
err
PROG_D
>>> re.search (r'call fun \((.+?),(.+?),(.*)\)', str1, re.DOTALL).groups()
('TP$NULL', ' \n ERROR_CODE', "\n PROG_ID || 'Password Incorrect, Please try again'")
should cover both presented cases to an acceptable state 应该将两种情况都覆盖到可接受的状态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.