考虑字符串:
<p class="sm clg" data-rlocation="Uttam Nagar East">Uttam Nagar East, Delhi <span class="to-txt" id="citytt1">B-24, East Uttam Nagar, Uttam Nagar East,<br>Delhi<span> - </span>110059
我想用regex函数得到结果Uttam Nagar East
,但得到的结果是
Uttam Nagar East">Uttam Nagar East, Delhi <span class="to-txt" id="citytt1'
我试过用
print(re.findall(r'data-rlocation="(.*)["]',contents))
以及
print(re.findall(r'data-rlocation="(.*)"',contents))
组
(.*)
在其捕获中包含右引号。请尝试以下操作:看看它是如何工作的here。你知道吗
默认情况下,
*
是贪婪的,这意味着它试图使用尽可能多的字符。如果希望匹配尽可能少的字符,可以使用非贪婪限定符*?
:更多信息:https://docs.python.org/3.5/howto/regex.html#greedy-versus-non-greedy
你正在使用贪婪的正则表达式,你可以加上“?”使它不贪婪
相关问题 更多 >
编程相关推荐