使用正则表达式获取指定元素 - PHP

get a specified element using regex - PHP

我正在尝试使用 regex.htaccess 文件中获取页面的名称,这是行:

RewriteRule ^test.html$                                     /public/index.php?test=

我正在尝试检索 test.html。这是我迄今为止尝试过但效果不佳的方法:

preg_match('/\b\^[\w%+\/-]+?$\b/', $input, $matches);

有什么帮助吗?当谈到正则表达式时,我很挣扎!非常感谢。

您需要从原始正则表达式中删除 \b(匹配单词字符和非单词字符)。因为 space^ 符号之间不存在单词边界。而且你还需要在 char class.

中包含点
preg_match('~\^([\w%+\/.-]+?)$~', $input, $matches);

使用上面的正则表达式,然后从组索引 1 中获取您想要的字符串。

DEMO

使用环顾四周。

preg_match('~(?<=\^)[\w%+\/.-]+?(?=$)~', $input, $matches);

DEMO