用于匹配传递的 Function/Method 参数的正则表达式

Regex to Match Passed Function/Method Parameters

我仔细看了看以前问过的问题;唉,我搜索 PHP preg_match 没有返回任何结果(也许我的搜索技巧不够,我想 合理 考虑到这是一个正则表达式问题!)。 =30=]

考虑以下文字:

The quick __("brown ") fox jumps __('over the') lazy __("dog")

现在我需要 'scan' 上面给定的方法 __(''),而它可能包括间距和不同的引号 ('|")。经过无数次的我最好的尝试 'iterations':

(__\("(.*?)"\))|(__\('(.*?)'\))

或最简单的形式:

__\((.*?)\)

分解:

但是,这只能得到部分匹配,而空格完全会影响搜索。抱歉,如果之前有人问过这个问题,请 link 我如果是这样的话!

上面提供的模式的测试者Link:

PHP Live Regex Test Tool

这个怎么样:

 (__(\('[^']+'\)|\("[^"]+"\)))

而不是非贪婪 .,使用引号 [^'][^"]

以外的任何字符

当搜索到的方法字符串使用单引号时,与使用双引号相比,它将在另一个捕获组中结束。所以实际上,您的正则表达式有效(空格除外,请往下看),但您必须查看结果数组中的不同索引:

$input = 'The quick __("brown ") fox jumps __(\'over the\') lazy __("dog")';

// using your regular expression:
$res = preg_match_all("/(__\(\"(.*?)\"\))|(__\('(.*?)'\))/", $input, $matches);

print_r ($matches);

请注意,您需要 preg_match_all 而不是 preg_match 才能获得所有匹配项。

输出:

Array
(
    [0] => Array
        (
            [0] => __("brown ")
            [1] => __('over the')
            [2] => __("dog")
        )
    [1] => Array
        (
            [0] => __("brown ")
            [1] => 
            [2] => __("dog")
        )
    [2] => Array
        (
            [0] => brown 
            [1] => 
            [2] => dog
        )
    [3] => Array
        (
            [0] => 
            [1] => __('over the')
            [2] => 
        )
    [4] => Array
        (
            [0] => 
            [1] => over the
            [2] => 
        )
)

因此,结果数组有 5 个元素,第一个代表完全匹配,所有其他元素对应于正则表达式中的 4 个捕获组。由于单引号的捕获组不是双引号的捕获组,因此您会在不同的地方找到匹配项。

为了 "solve" 这个,你可以在你的正则表达式中使用反向引用,它会回头看看哪个是开头引号(单引号或双引号)并要求在最后重复相同的内容:

$res = preg_match_all("/__\(([\"'])(.*?)\1\)/", $input, $matches);

注意反向引用 </code>(反斜杠必须用另一个反斜杠转义)。这指的是第一个捕获组,我们有 <code>["'](再次需要转义)来匹配两种引号。

您还想处理空格。在您的 PHP Live Regex 上,您使用了一个测试字符串,该字符串在括号和引号之间有这样的空格。为了处理这些以便它们仍然正确匹配方法字符串,正则表达式应该得到两个额外的 \s*:

$res = preg_match_all("/__\(\s*([\"'])(.*?)\1\s*\)/", $input, $matches);

现在输出是:

Array
(
    [0] => Array
        (
            [0] => __("brown ")
            [1] => __('over the')
            [2] => __("dog")
        )
    [1] => Array
        (
            [0] => "
            [1] => '
            [2] => "
        )
    [2] => Array
        (
            [0] => brown 
            [1] => over the
            [2] => dog
        )
)

...现在组捕获的文本排列得很好。

eval.in and PHP Live Regex 上查看此代码 运行。

当处理这样的东西时,不要忘记转义:

<?php
ob_start();
?>

The quick __("brown ") fox jumps __(  'over the'  ) lazy __("dog").
And __("everyone says \"hi\"").

<?php

$content = ob_get_clean();

$re = <<<RE
    /__ \(
        \s*
        " ( (?: \\. | [^"])+ ) "
        |
        ' ( (?: \\. | [^'])+ ) '
        \s*
    \)
    /x
RE;

preg_match_all($re, $content, $matches, PREG_SET_ORDER);
foreach($matches as $match)
    echo end($match), "\n";

用方括号将双引号和单引号括起来作为一个字符 class:

$str = 'The quick __( "brown ") fox jumps __(\'over the\') lazy __("dog")';

preg_match_all("/__\(\s*([\"']).*?\1\s*\)/ium", $str, $matches);

echo '<pre>';
var_dump($matches[0]);

// the output:
array (size=3)
    0 => string '__( "brown ")' 
    1 => string '__('over the')'
    2 => string '__("dog")'

下面是在 phpliveregex.com 上使用相同解决方案的示例:
http://www.phpliveregex.com/p/exF (节 preg_match_all