PHP preg_match_all()匹配太多
我有一个名为PHP preg_match_all()匹配太多,php,regex,Php,Regex,我有一个名为$content的变量,它包含降价文件的内容 我正在尝试匹配样式为的所有链接:[[http://url.com/|标题]] 以下是我试图匹配的变量的一部分: [[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]], [[https://msdn.microsoft.com/en-us/library
$content
的变量,它包含降价文件的内容
我正在尝试匹配样式为的所有链接:[[http://url.com/|标题]]
以下是我试图匹配的变量的一部分:
[[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]], [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]] and [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]
我当前的正则表达式是:/\[\[(.*)\\\\.([\w\s]+?)\]\](?=\,|\s)/
但它与我上面列出的整个部分相匹配,包括、
和和
我想要的是分开的每个链接,因此我从preg_match_all(“/regular_expression/”,$content,$links)中查找的结果代码>是:
$links[0][0] = [[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]]
$links[0][1] = [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]]
$links[0][2] = [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]
$links[1][0] = http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm
$links[1][1] = https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx
$links[1][2] = https://www.jetbrains.com/idea/help/managing-bookmarks.html
$links[2][0] = Eclipse
$links[2][1] = Visual Studio
$links[2][2] = IntelliJ Idea
我想这就是你想要的:
$string = '[[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]], [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]] and [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]';
preg_match_all('/\[{2}(.+?)\|([\w\s]+?)\]{2}/', $string, $links);
print_r($links);
输出:
Array
(
[0] => Array
(
[0] => [[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]]
[1] => [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]]
[2] => [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]
)
[1] => Array
(
[0] => http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm
[1] => https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx
[2] => https://www.jetbrains.com/idea/help/managing-bookmarks.html
)
[2] => Array
(
[0] => Eclipse
[1] => Visual Studio
[2] => IntelliJ Idea
)
)
Regex101演示:
只需要你的*
是非贪婪的?
并且你可能想要在|
之前的东西,所以让量词成为+
(一个或多个);如果你不在乎是否有任何东西,你可以把它改回*
。我想这就是你想要的:
$string = '[[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]], [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]] and [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]';
preg_match_all('/\[{2}(.+?)\|([\w\s]+?)\]{2}/', $string, $links);
print_r($links);
输出:
Array
(
[0] => Array
(
[0] => [[http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm|Eclipse]]
[1] => [[https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx|Visual Studio]]
[2] => [[https://www.jetbrains.com/idea/help/managing-bookmarks.html|IntelliJ Idea]]
)
[1] => Array
(
[0] => http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-37-3c.htm
[1] => https://msdn.microsoft.com/en-us/library/xc3ed5eh%28v=vs.90%29.aspx
[2] => https://www.jetbrains.com/idea/help/managing-bookmarks.html
)
[2] => Array
(
[0] => Eclipse
[1] => Visual Studio
[2] => IntelliJ Idea
)
)
Regex101演示:
只需要你的*
是非贪婪的?
并且你可能想要在|
之前的东西,所以让量词成为+
(一个或多个);如果你不在乎那里是否有任何东西,你可以把它改回*
。把:(.*)
(这太贪婪和不精确了)改成:([^ |\[\]]*)
。问题和答案?不,我知道你改变了什么,我实现了它,效果很好。非常感谢你!将:(.*)
(这太贪婪和不精确)更改为:([^ 124; \[\]]]*)
。问题或问题的答案?不,我理解您所做的更改,我实施了它,效果很好。非常感谢你!