python - 在python中使用正则表达式匹配字符串的开头和结尾

标签 python regex

我正在尝试从这个 webpage 中提取可解析的引用信息使用 python 。例如,对于列出的页面,我将提取 pl/111/148 和 pl/111/152。下面列出了我当前的正则表达式,但它似乎在可解析引用之后返回了所有内容。这可能很简单,但我对正则表达式比较陌生。提前致谢。

re.findall(r'^parsable-cite=.*>$',page)

最佳答案

我强烈推荐使用这个正则表达式,它将捕获你想要的内容:

re.findall(r'parsable-cite=\\\"(.*?)\\\"\>',page)

解释:

parsable-cite= matches the characters parsable-cite= literally (case sensitive)
  \\ matches the character \ literally
  \" matches the character " literally
  1st Capturing group (.*?)
  .*? matches any character (except newline)
      Quantifier: Between zero and unlimited times, as few times as possible,
           expanding as needed
  \\ matches the character \ literally
  \" matches the character " literally
  \> matches the character > literally

使用 ? 是关键;)

希望这对您有所帮助。

关于python - 在python中使用正则表达式匹配字符串的开头和结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22699040/

相关文章:

python - 给定一个描述 object.attribute 的 Python 字符串,如何将属性的命名空间与属性分开?

Python - 使用数据库中的图表创建 pdf 报告的过程是什么?

python - 故障排除 "descriptor ' 日期'需要 'datetime.datetime' 对象,但收到 'int'“

python - 从 python panda 数据框中的大量文本中逐行删除 URL

ruby-on-rails - 正则表达式使 Ruby 完全卡住

javascript - 基于分隔符/方括号的实例创建数组

python pandas基于2个键合并数据

python - 为什么 python 从文件中打印空行

java - 如何从字符串中删除所有非字母数字字符而不删除@符号

regex - 如何使用正则表达式使用 Powershell 替换文本文件中的日期