python - 结束行未使用 re library python 正确解析

标签 python regex

考虑字符串:

<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/

相关文章:

python - Django 1.8 错误 : No database fixture specified

Python 2.7 嵌套的 If 语句在打印数组位置后不会打印字符串文字

java - 用java分割字符串不起作用

javascript - 从 Word 复制时从剪贴板数据中删除项目符号

java - API 21(android 5) 及以下版本上的正则表达式模式错误

正则表达式到有限自动机

python - python方法签名中的_=None有什么意义?

python - threading.current_thread() 中的 Dummy 是什么?

python - Keras:模型加载后如何设置学习阶段

python - 正则表达式: extract text between two markers