我有一个名为$content的变量,它包含DokuWiki Markdown文件的内容。
我试图匹配样式为:[[http://url.com/|title]]的所有链接。
下面是我试图与之匹配的变量的一部分:
[[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)/,但它与我前面列出的整个部分相匹配,包括,和and。
我想要的是每个链接都分开,所以我要从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发布于 2015-11-02 23:55:09
我想这就是你想要的:
$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演示:https://regex101.com/r/iM4kG3/1
只需要你的*是非贪婪的?,你可能在|之前想要一些东西,所以把量词变成+ (一个或多个);如果你不在乎那里有什么东西,可以把它改为*。
https://stackoverflow.com/questions/33488576
复制相似问题