【发布时间】:2015-08-20 14:03:13
【问题描述】:
我正在尝试从长文本中提取文件名。
- 文件名都在一个路径中
- 路径始终以文本
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 部分之前的最后一个/?
【问题讨论】:
-
我觉得问这个比我有更多代表的人很愚蠢,但是你使用的是哪个正则表达式引擎?你用什么语言工作?
-
第一个似乎还可以:regex101.com/r/pJ4cH3/1你能更准确地描述这个问题吗?
-
我会说regex101.com/r/dH3vI5/1。即
(?:Page source).*\/(.*.html)。 -
@Antwane 和 fedorqui 感谢您的意见。我的示例文本与我的真实文本并不完全一致,因此在我的实际文本中不知何故它不起作用。真的不能把我的手指放在它上面。
标签: regex