【发布时间】:2021-02-20 21:54:49
【问题描述】:
您好,正则表达式向导!,
我有一个包含多个 guid 分布在多行上的文档。我需要用名称为找到的 guid 的文件中的内容替换这些 guid。我试过这个:
preg_replace('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', file_get_contents("${1}.php"), $lines);
但它无法找到 guid(因此,还抱怨找不到 ${1})。
这里是多行字符串:
"
// Code in page
//Run requested command:
37906e3a-62f3-4d83-a8e0-9ad64a6a5228
de2dd82d-df13-4a8f-a760-cbc6e3db29d0
// bindings from HTML
af6236de-cf5d-447b-a9d5-28549aebd0fa
"
我做错了什么?
谢谢, 视频
【问题讨论】:
-
preg_replace_callback('/^(?:\s?[a-f\d]){8}\s?(?:-(?:\s?[a-f\d]){4}){4}(?:\s?[a-f\d]){8}$/mi', function($m) { return file_get_contents(preg_replace("~\s+~", "", $m[0]) . ".php"); }, $lines);?见the regex demo。
标签: php regex preg-replace