正则表达式与捕获组

时间:2015-06-05 14:53:12

标签: regex

我试图从长文本中提取文件名。

  • 文件名都在路径中
  • 路径始终以文本Page source
  • 作为前缀
  • 它们可以出现在任何地方
  • 文字包含多行
  • 所有文件名以.html
  • 结尾

给出以下文字:

Page source file:///somedir/subdir/subdir/mysource.html lorem ipsum more text
Lorem Ipsum ...
Lorem Ipsum Page source file:///anotherdir/sub/dir/anothersource.html

我想要一个包含所有文件名的列表:

mysource.html
anothersource.html

我一直试图通过以下正则表达式来获取它:

// this only gets the last one (because of the greedy .*)
Page source.*\/(.*\.html)

// This gets all occurrences, but the value in my capture group is the 
// complete path starting after the first occurrence of /
Page source.*?\/(.*?\.html)

我如何告诉正则表达式引擎对外表达式不贪心,但仍然贪婪到/部分之前的最后.html

1 个答案:

答案 0 :(得分:7)

Page source.*?([^\/]+?\.html)

演示:https://regex101.com/r/uX6fY2/2

相关问题