GVKun编程网logo

php – 如何使用preg_match_all()获取子组匹配的所有捕获?(php获取数组指定值的key)

25

针对php–如何使用preg_match_all()获取子组匹配的所有捕获?和php获取数组指定值的key这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展(PHP)正则表达式-preg_ma

针对php – 如何使用preg_match_all()获取子组匹配的所有捕获?php获取数组指定值的key这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展(PHP)正则表达式-preg_match和preg_match_all()的用法、php preg_match_all 与preg_match 区别与实例、php preg_match_all()函数使用实例、php preg_match_all匹配文章中图片等相关知识,希望可以帮助到你。

本文目录一览:

php – 如何使用preg_match_all()获取子组匹配的所有捕获?(php获取数组指定值的key)

php – 如何使用preg_match_all()获取子组匹配的所有捕获?(php获取数组指定值的key)

参见英文答案 > Get repeated matches with preg_match_all()                                    6个
更新/注意事项:

I think what I’m probably looking for is to get the 07001 in PHP.

参考文献:PCRE regular expressions using named pattern subroutines.

(仔细读:)

我有一个包含可变数量的段(简化)的字符串:

$subject = 'AA BB DD '; // Could be 'AA BB DD CC EE ' as well

我想现在匹配段并通过匹配数组返回它们:

$pattern = '/^(([a-z]+) )+$/i';
$result = preg_match_all($pattern,$subject,$matches);

这将仅返回捕获组2的最后一个匹配:DD.

有没有办法可以通过一次正则表达式执行检索所有子模式捕获(AA,BB,DD)? preg_match_all不适合这个吗?

这个问题是一个概括.

$subject和$模式都被简化了.当然,使用这样的AA,…的一般列表更容易用其他函数(例如,爆炸)或使用$pattern的变体来提取.

但我特别询问如何使用preg _…-系列函数返回所有子组匹配.

对于现实生活中的情况,假设您有多个(嵌套)级别的子模式匹配变量.

这是用于描述一些背景的伪代码的示例.想象一下:

令牌的常规定义:

CHARS := [a-z]+
   PUNCT := [.,!?]
   WS := [ ]

$subject得到基于这些的标记.标记化存储在标记数组(type,offset,…)中.

然后将该数组转换为字符串,每个标记包含一个字符:

CHARS -> "c"
   PUNCT -> "p"
   WS -> "s"

因此,现在可以在令牌流字符串索引上运行基于令牌(而不是字符类等)的正则表达式.例如.

regex: (cs)?cp

表达一组或多组字符,后跟标点符号.

我现在可以将自定义标记表达为正则表达式,下一步是构建语法.这只是一个例子,这是一种ABNF风格:

words = word | (word space)+ word
   word = CHARS+
   space = WS
   punctuation = PUNCT

如果我现在将单词的语法编译成(标记)正则表达式,我希望自然地拥有每个单词的所有子组匹配.

words = (CHARS+) | ( (CHARS+) WS )+ (CHARS+)    # words resolved to tokens
  words = (c+)|((c+)s)+c+                         # words resolved to regex

我可以编码直到这一点.然后我遇到了子组匹配的问题只包含他们的最后一场比赛.

所以我可以选择自己创建一个语法自动机(我想阻止语法表达式保持通用)或者某种程度上让preg_match为我工作,所以我可以省去它.

这基本上都是.可能现在可以理解为什么我简化了这个问题.

有关:

> pcrepattern man page
> Get repeated matches with preg_match_all()

解决方法

试试这个:

preg_match_all("'[^ ]+'i",$text,$n);

$n [0]将包含文本中所有非空格字符组的数组.

编辑:包含子组:

preg_match_all("'([^ ]+)'i",$n);

现在$n [1]将包含子组匹配,与$n [0]完全相同.实际上这毫无意义.

Edit2:嵌套子组示例:

$test = "Hello I'm Joe! Hi I'm Jane!";
preg_match_all("/(H(ello|i)) I'm (.*?)!/i",$test,$n);

结果如下:

Array
(
    [0] => Array
        (
            [0] => Hello I'm Joe!
            [1] => Hi I'm Jane!
        )

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

    [2] => Array
        (
            [0] => ello
            [1] => i
        )

    [3] => Array
        (
            [0] => Joe
            [1] => Jane
        )

)

(PHP)正则表达式-preg_match和preg_match_all()的用法

(PHP)正则表达式-preg_match和preg_match_all()的用法

<?php
/**
 * 正则表达式练习
 * User: Ollydebug
 * Date: 2015/11/13
 * Time: 13:28
 */

/*
 * preg_match()第三个参数可选,第三个参数是引用传递,它在匹配subject的时候,只会匹配一次
 * preg_match_all()第三个参数必填,第三个参数也是引用传递,它在匹配subject的时候,会把所有满足条件的结果都匹配出来
 */

$pattern = &#39;/[0-9]/&#39;;
$subject = &#39;weuyr3ui76as83s0ck9&#39;;
$m1 = $m2 = array();
$t1 = preg_match($pattern,$subject,$m1);
$t2 = preg_match_all($pattern,$subject,$m2);

show($m1);
echo &#39;<hr/>&#39;;
show($m2);
echo &#39;<hr/>&#39;;
show($t1.&#39;||&#39;.$t2);

function show($var){
    if(empty($var)){
        echo &#39;null&#39;;
    }elseif(is_array($var)||is_object($var)){
        // array,object
        echo &#39;<pre>&#39;;
        print_r($var);
        echo &#39;
登录后复制
''; }else{ //string,int,float echo $var; } } ?>

 以上就是(php)正则表达式-preg_match和preg_match_all()的用法的内容,更多相关内容请关注php中文网(www.php.cn)!

php preg_match_all 与preg_match 区别与实例

php preg_match_all 与preg_match 区别与实例

<script>ec(2);</script>

/*

 代码如下 复制代码
int preg_match_all ( string $pattern , string $subject , array &$matches [, int $flags [, int $offset ]] );

搜索所有匹配正则表达式的模式并提出给予他们在比赛中受的标志指定的顺序。第一场比赛后发现,随后的搜查是继续从最后一场比赛结束。

实例

 代码如下 复制代码
preg_match_all("|]+>(.*)[^>]+>|u",
    "example:
this is a test
",
    $out, preg_pattern_order);
echo $out[0][0] . ", " . $out[0][1] . " ";
echo $out[1][0] . ", " . $out[1][1] . " ";

出输

 代码如下 复制代码

example: ,

this is a test

example: , this is a test


preg_match_all("|]+>(.*)[^>]+>|u",
    "example:

this is a test
",
    $out, preg_set_order);
echo $out[0][0] . ", " . $out[0][1] . " ";
echo $out[1][0] . ", " . $out[1][1] . " ";

 

立即学习“PHP免费学习笔记(深入)”;

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
搜索主题的经常表达的方式给予配合


$subject = "abcdefwww.111cn.net";
$pattern = ''/^def/'';
preg_match($pattern, substr($subject,3), $matches, preg_offset_capture);
print_r($matches);


array
(
    [0] => array
        (
            [0] => def
            [1] => 0
        )

)


*/

php preg_match_all()函数使用实例

php preg_match_all()函数使用实例

preg_match_all() 函数用于执行一个全局正则表达式匹配.

preg_match_all() 将实现全部结果的匹配,如果要匹配成功一次后停止匹配,请使用 preg_match() 函数。

语法:

preg_match_all (pattern,subject,matches)

参数

描述

pattern

正则表达式

subject

进行匹配的字符串

matches

所有匹配结果(数组)

实例:

$con = file_get_contents("http://bbs.it-home.org/news/jb-1.html");

  • $pattern="//";
  • preg_match_all($pattern,$con,$match);
  • print_r($match);
  • ?>
  • 复制代码

    输出结果: Array ( [0] => Array ( [0] => 脚本学堂 [1] => [2] => ) [1] => Array ( [0] => http://bbs.it-home.org/usr/themes/dddefault/images/logo.png [1] => http://bbs.it-home.org/usr/uploads/2012/09/531656480.jpg [2] => http://bbs.it-home.org/usr/uploads/2012/09/2647136297.jpg ) )



    关于php – 如何使用preg_match_all()获取子组匹配的所有捕获?php获取数组指定值的key的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于(PHP)正则表达式-preg_match和preg_match_all()的用法、php preg_match_all 与preg_match 区别与实例、php preg_match_all()函数使用实例、php preg_match_all匹配文章中图片等相关内容,可以在本站寻找。

    本文标签:

    上一篇php – MCrypt rijndael-128到OpenSSL aes-128-ecb转换(php转码)

    下一篇无法重新声明函数php(无法重新声明块范围变量)