考虑字符串:
<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
我想使用正则表达式函数得到结果 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))
最佳答案
(.*)
组在其捕获中包含结束引号。试试这个:
>>> re.findall(r'data-rlocation="([^"]*)"', contents)
['Uttam Nagar East']
查看其工作原理 here .
关于python - 结束行未使用 re library python 正确解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57898350/