【发布时间】:2017-06-03 12:52:47
【问题描述】:
我想提取“;”之间的单词和 XML 文件中的“:”,例如此处的“索引”一词
bla bla bla ;索引 : bla bla
文件通过其 URL 使用 file_get_contents 加载
$output = file_get_contents("https://fr.wikipedia.org/wiki/Sp%C3%A9cial:Exporter/Base_de_donn%C3%A9es");
preg_match_all('/\;.[a-zA-Z]+.\:/', $output, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
正则表达式模式在使用regex101 的相同文件内容上以及当我将文本复制到字符串变量中时也可以正常工作。但是上面的代码不起作用,它只返回最后一个匹配项。
我做错了什么?
PS:我还尝试使用 DOMDocument 加载 XML 文件。结果相同。
【问题讨论】:
-
你应该这样做
/;[^:;]+:/ -
如果我理解你想提取:
Index,Vue matérialisée,Partitionnement,[[RAID (informatique)|RAID]] (''Redundant array of inexpensive disks''),Table de Hashage (anglais ''hashing''),...,Journal -
我只想提取单词或一组单词,没有任何特殊字符或数字。这就是我使用 [a-zA-Z] 的原因
-
我使用的正则表达式工作正常,这不是问题..
-
@sweaver2112:注意这是一个 PHP 问题,而不是 Python 问题。
标签: php regex string preg-match file-get-contents