Regex PRCE PHP preg_match_all:如何删除匹配数组中的空节点?

发布于 2024-10-24 19:11:53 字数 1034 浏览 2 评论 0原文

$text = 'Lorem Ipsum';
$re = '/(?<AA>Any)|(?<BB>Lorem)/ui';
$nMatches = preg_match_all($re, $text, $aMatches);

$aMatches 将包含以下内容:

Array (
    [0] => Array (
            [0] => Lorem
        )

    [AA] => Array (       //  do not include to result matches array
            [0] =>        //  because have not match for this part
        )

    [1] => Array (
            [0] => 
        )

    [BB] => Array (
            [0] => Lorem
        )

    [2] => Array (
            [0] => Lorem
        )
)

问题: 对于没有匹配的命名部分,是否可以返回没有节点的数组?

 Array (
        [0] => Array (
                [0] => Lorem
            )

        {there was [AA] && [1], but have not returned because empty} 

        [BB] => Array (
                [0] => Lorem
            )

        [1] => Array (        // changed to 1
                [0] => Lorem
            )
    )
$text = 'Lorem Ipsum';
$re = '/(?<AA>Any)|(?<BB>Lorem)/ui';
$nMatches = preg_match_all($re, $text, $aMatches);

$aMatches will contain the following:

Array (
    [0] => Array (
            [0] => Lorem
        )

    [AA] => Array (       //  do not include to result matches array
            [0] =>        //  because have not match for this part
        )

    [1] => Array (
            [0] => 
        )

    [BB] => Array (
            [0] => Lorem
        )

    [2] => Array (
            [0] => Lorem
        )
)

Question:
Is it possible to return the array without nodes for named parts that have no matches?

 Array (
        [0] => Array (
                [0] => Lorem
            )

        {there was [AA] && [1], but have not returned because empty} 

        [BB] => Array (
                [0] => Lorem
            )

        [1] => Array (        // changed to 1
                [0] => Lorem
            )
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-10-31 19:11:53

特别是对于正则表达式,您可以看到 @Stephan 的回答。更一般地,当仅操作数组时,您可以使用 array_maparray_filter 来做到这一点。不带回调的 array_filter 将删除计算结果为 false 的值(== false 不是 === false,请参阅)。

对于单层数组:

$array = array('foo', '', 'bar');
$clean_array = array_filter($array);

对于二维数组:

$clean_array = array_filter(array_map('array_filter', $array));

Specifically for regex, you can see @Stephan's answer. More generally, when just manipulating arrays, you can use a combination of array_map and array_filter to do that. array_filter without a callback will strip the values that evaluates to false (== false not === false, see empty).

For a single-level array:

$array = array('foo', '', 'bar');
$clean_array = array_filter($array);

For a 2D array:

$clean_array = array_filter(array_map('array_filter', $array));
单挑你×的.吻 2024-10-31 19:11:53

您可以通过使用分支重置运算符来实现类似的效果:

/(?|(?Any)|(?Lorem))/ui

输出

  • [0]=> ;大批
    • [0]=>Lorem
  • [BB]=>数组
    • [0]=>Lorem
  • [1]=> array
    • [0]=>Lorem

通过使用此运算符,命名组必须具有相同的名称。

您测试正则表达式此处

You can achieve something similar by using the branch-reset operator :

/(?|(?<BB>Any)|(?<BB>Lorem))/ui

outputs

  • [0]=> array
    • [0]=>Lorem
  • [BB]=> array
    • [0]=>Lorem
  • [1]=> array
    • [0]=>Lorem

By using this operator the named groups must have the same name.

You test the regexp here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文