PHP 正则表达式 多行

发布于 2024-10-14 01:30:15 字数 503 浏览 9 评论 0原文

我有以下 PHP 正则表达式

preg_match('/[\/\/\*] First: (.*)\n[\/\/\*] Second: (.*)\n/i', $some_string)

,但由于某种原因它与此文本不匹配:

// First: a string
// Second: another string

我尝试更改 Windows 和 Unix 风格之间的行结尾,但这没有任何作用。我还尝试拆分正则表达式以分别匹配 First 和 Second;这有效,但是当我将它们放在一起时,它们不再与示例文本匹配。它似乎与第二个 [\/\/\*] 之后的空格有关..有什么想法吗?

注意我无法更改正则表达式;这是我逆转的客户端代码,因为他们不提供文档。此代码在 PHP 文件中查找特定模式,以便将它们作为“插件”加载到其产品中。实际上,我试图猜测我需要添加到这些 PHP 文件中的标头,以便它们能够正确地被识别为插件。

I have the following PHP regex

preg_match('/[\/\/\*] First: (.*)\n[\/\/\*] Second: (.*)\n/i', $some_string)

but for some reason it will not match this text:

// First: a string
// Second: another string

I tried changing line endings between Windows and Unix style, this did nothing. I also tried splitting up the regex to match First and Second separately; this worked but when I put them both together they no longer match the sample text. It seems to have something to do with the space after the second [\/\/\*].. any ideas?

Note I can't change the regex; this is client code that I reversed because they don't provide documentation. This code looks for certain pattern in PHP files in order to load them as 'plugins' in their product. Really I'm trying to guess what header I need to add to these PHP files so they will correctly be recognized as plugins.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被翻牌 2024-10-21 01:30:15
$some_string = <<<STR
// First: a string
// Second: another string

STR;

$match = preg_match('`// First: (.*)\n// Second: (.*)\n`i', $some_string);

echo $match;

刚刚测试了这个,它有效。您确定第二行之后有换行符吗?

$some_string = <<<STR
// First: a string
// Second: another string

STR;

$match = preg_match('`// First: (.*)\n// Second: (.*)\n`i', $some_string);

echo $match;

Just tested this, it works. Are you sure there's a linebreak after the second line?

把人绕傻吧 2024-10-21 01:30:15

(代表 OP 发布)

答案:

我试图猜测这会匹配什么模式。结果是这样的:

/*
* First: a string
* Second: another string
*/

(Posted on behalf of the OP).

Answer:

I was trying to guess what pattern this would match. It ended up being this:

/*
* First: a string
* Second: another string
*/
残疾 2024-10-21 01:30:15

尝试:

preg_match('~// First: (.*)\n// Second: (.*)~i', $str);

查看

为什么你的正则表达式是错误的?

[\/\/\*] 是匹配 /* 的字符类。

但是您似乎在字符串的开头有 // ,因此您将永远获得匹配项。

我当前的正则表达式将匹配什么字符串?

将当前输入更改为在开头有一个 / 并在第二行之后有一个换行符:

$some_string =
'/ First: a string
/ Second: another string
';

查看

Try:

preg_match('~// First: (.*)\n// Second: (.*)~i', $str);

See it

Why is your regex wrong?

[\/\/\*] is a character class that either matches a / or a *.

But you seem to have // at the beginning of the string, so you'll never get a match.

What string will my current regex match?

Change your current input to have a / at the beginning and a newline after the second line:

$some_string =
'/ First: a string
/ Second: another string
';

See it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文