简体   繁体   English

带有\\ +?=的正则表达式无法正常工作/转义'+'

[英]Regex with \+?= is not working / escaping '+' as expected

I have the following code: 我有以下代码:

import re
pattern = r'.className\s*\+?=\s*.*?\+?[\'"](.*?)["\']'
code_str = "if (El.className == 'blue' ) {\nEl.className = 'className20';\n}\n"
re.findall(pattern, code_str)

Output >>> ['blue', 'className20'] 输出>>> ['blue', 'className20']

I am only wanting the output ['className20'] . 我只想要输出['className20']

[ Update ] It also needs to handle this concatenation case where some_str is combined with className20 . [ 更新 ]还需要处理some_strclassName20组合的这种串联情况。 It should still only return ['className20'] . 它仍然应该只返回['className20']

code_str2 = "if (El.className == 'blue' ) {\nEl.className = some_str + 'className20';\n}\n"
re.findall(pattern, code_str)

I've also tried. 我也尝试过

pattern = r'.className\s*\+{,1}={1}\s*.*?\+?[\'"](.*?)["\']'
pattern = r'.className\s*\+?[=]{1}\s*.*?\+?[\'"](.*?)["\']'

Any insight is welcomed. 欢迎任何见识。 The problem seems to be that == appears in code_str . 问题似乎是==出现在code_str The pattern I'm using is too greedy. 我使用的模式过于贪婪。 My understanding is that \\+ is properly escaping the + sign, but I could be wrong. 我的理解是\\+正确地转义了+号,但我可能错了。

Why is there a .*? 为什么会有.*? after =\\s* ? =\\s* You want to allow anything to follow an equals followed by any amount of space? 您想让任何东西跟随等号后跟任意数量的空间吗? That's what allows == 'blue' to be accepted; 这就是允许== 'blue'被接受的原因; the \\s* does nothing, but the following .*? \\s*除了以下.*?都不做.*? is matching the = following the first = . 与第一个=之后的=匹配。 Drop it, and it works: 删除它,就可以了:

import re
pattern = r'.className\s*\+?=\s*\+?[\'"](.*?)["\']'
code_str = "if (El.className == 'blue' ) {\nEl.className = 'className20';\n}\n"
re.findall(pattern, code_str)

producing ['className20'] as expected. 按预期产生['className20'] The question is whether there was some reason to allow arbitrary characters there. 问题是,是否存在某些允许在其中允许任意字符的原因。

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

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