首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP preg_match_all()匹配过多

PHP preg_match_all()匹配过多
EN

Stack Overflow用户
提问于 2015-11-02 22:59:24
回答 1查看 111关注 0票数 1

我有一个名为$content的变量,它包含DokuWiki Markdown文件的内容。

我试图匹配样式为:[[http://url.com/|title]]的所有链接。

下面是我试图与之匹配的变量的一部分:

代码语言:javascript
复制
[[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);中寻找的结果是:

代码语言:javascript
复制
$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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-02 23:55:09

我想这就是你想要的:

代码语言:javascript
复制
$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);

输出:

代码语言:javascript
复制
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

只需要你的*是非贪婪的?,你可能在|之前想要一些东西,所以把量词变成+ (一个或多个);如果你不在乎那里有什么东西,可以把它改为*

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33488576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档