我有一个名为$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
答案 0 :(得分:0)
我认为这就是你所追求的目标:
$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
只需要你的*
非贪婪?
而你可能想要|
之前的某些内容,所以要使量词为+
(一个或多个);如果您不在乎是否有任何东西,可以将其更改回*