【问题标题】:php preg_match returns only matched portion of string when I am certain that there is an unmatched portion too当我确定也有不匹配的部分时,php preg_match 仅返回字符串的匹配部分
【发布时间】:2011-01-31 04:57:25
【问题描述】:

所以我试图获取所有不匹配的文本,并且我的代码 sn-ps 看起来像: 行,列 = (1, 1) 行,列 = (1, 2) 行,列 = (9999999, 9999999) 存储在一个名为 $foo 的数组中,我的 preg_match 语句如下所示:

foreach($foo as $fooPartition){
   if(preg_match("/.*\([0-9]+\, /", $fooPartition, $match)){
      $unecessaryFluff = $match[0];
      $infoINeed = $match[1];
   }
}

我不断返回 $unecessaryFluff,但是当我尝试访问 $match[1] 时,我得到了一个丢失的索引,并且我的调试器验证 $match 实际上只是一个包含字符串匹配部分的长度为 1 的数组,和 $match[1] DNE。

有谁知道我做错了什么使 preg_match 不根据 php 页面规范返回字符串的不匹配部分? (使用 php 5.3.2)

【问题讨论】:

    标签: php preg-match


    【解决方案1】:

    如果要隔离特定字符串,则需要指定分组表达式(使用括号)。

    例如,假设您只想要数字

    /.*\(([0-9]+)\, /
    

    不知道你为什么要转义逗号,这是一个错字吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2012-06-15
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多