PHP 正则表达式 多行
我有以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
刚刚测试了这个,它有效。您确定第二行之后有换行符吗?
Just tested this, it works. Are you sure there's a linebreak after the second line?
(代表 OP 发布)。
答案:
我试图猜测这会匹配什么模式。结果是这样的:
(Posted on behalf of the OP).
Answer:
I was trying to guess what pattern this would match. It ended up being this:
尝试:
查看
为什么你的正则表达式是错误的?
[\/\/\*]
是匹配/
或*
的字符类。但是您似乎在字符串的开头有
//
,因此您将永远获得匹配项。我当前的正则表达式将匹配什么字符串?
将当前输入更改为在开头有一个
/
并在第二行之后有一个换行符:查看
Try:
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:See it