Sha*_*kan 0 python regex pipe findall
x = "type='text'"
re.findall("([A-Za-z])='(.*?)')", x) # this will work like a charm and produce
# ['type', 'text']
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是我想实现一个管道(交替),以便相同的正则表达式适用
x = 'type="text"' # see the quotes
Run Code Online (Sandbox Code Playgroud)
基本上,以下正则表达式应该可以工作但是findall会导致一些奇怪的事情:
([A-Za-z])=('(.*?)')|"(.*?)")
Run Code Online (Sandbox Code Playgroud)
并且我不能使用['"]而不是管道,因为它可能以不良结果结束:
value="hey there what's up?"
Run Code Online (Sandbox Code Playgroud)
现在,我如何构建适用于单引号或双引号的正则表达式?顺便说一句,请不要建议任何html或xml解析器,因为我对它们不感兴趣.
shlex会在这里做得更好,但如果你坚持re,请使用([A-Za-z]+)=(?P<quote>['"])(.+?)(?P=quote)