Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式,从类似“quot;”的数组中筛选所需数据:键{value}";_Php_Regex - Fatal编程技术网

Php 正则表达式,从类似“quot;”的数组中筛选所需数据:键{value}";

Php 正则表达式,从类似“quot;”的数组中筛选所需数据:键{value}";,php,regex,Php,Regex,例如: $match_check = "(?'txt'(?<=:txt{)([^}]+)(?=}))|(?'reg'(?<=:reg{)([^}]+)(?=}))"; $route_from_value = ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}'; preg_match_all('/'.$match_check.'/', $route_from_value, $get_matchers_chec

例如:

$match_check = "(?'txt'(?<=:txt{)([^}]+)(?=}))|(?'reg'(?<=:reg{)([^}]+)(?=}))";

$route_from_value = ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}';

preg_match_all('/'.$match_check.'/', $route_from_value, $get_matchers_check);

var_dump($get_matchers_check);
但是,预期的结果应该是(如何仅使用regexp?)或者简单地说:

 'txt' =>
  array(4) {
    [0] =>
    string(8) "resultxt"
  },
 'txt' =>
  array(4) {
    [0] =>
    string(8) "resultxt"
  }
 'reg' =>
  array(4) {
    [0] =>
    string(8) "/^[a-zA-Z]*$/"
  }
 'reg' =>
  array(4) {
    [0] =>
    string(8) "regexresult"
  }

preg_match_all
preg_split
函数的输出格式与所需格式不匹配

您可以做的只是对
preg\u match\u all
的输出进行一些后处理,以获得结果(类似于
['key','value']
对的数组)。请注意,您应该设置标志以获取匹配项的索引以进行比较。不匹配的捕获组将给出索引
-1
,或者返回包含2个元素的数组

顺便说一下,您可以从正则表达式中删除一些不必要的捕获组

"(?'txt'(?<=:txt{)[^}]+(?=}))|(?'reg'(?<=:reg{)[^}]+(?=}))"

(?'txt'(?无法生成所需的输出。这只是因为
preg_match_all
没有以这种格式输出:请注意外部数组索引如何对应于捕获组,而内部数组索引如何对应于匹配。您正在使用正则表达式解析包含正则表达式的字符串?听起来非常复杂对于这一点,我不会使用regex,即对
进行拆分,然后对
{
}进行简单的字符串操作
可能是实现这一点的更简单的方法。出于某种原因,我正在编写一些复杂的脚本,可以用于另一个PHP类上的简单数据获取。我已经拆分了,但是,可以像
:txt
:reg
:num
等。实际上,如果字符串包含正则表达式,这是非常简单的rd正确解析字符串。如何知道
}
是正则表达式的一部分,还是外部分隔符的一部分?我正在使用的脚本使用闭包标记
True,我可以使用
key
获取数据,但我需要使用
array\u filter()
从结果中删除空数组。但是在
regex101
上,我使用的是同一个脚本,
regex101
是用PHP编写的页面,它正确地过滤了空数组和空值。这很奇怪,我认为当你使用
regex101
页面过滤所有空数组时,这是一个糟糕的解决方案,但你并不期望re不是另一个解决方案,或者我已经让我的生活复杂化了:(@MarinSagovac:Empty元素是正确的,因为在那些捕获组中没有捕获任何内容。在这种情况下,因为您知道匹配是互斥的(一个分支匹配,意味着其他分支将不匹配),对于每个内部索引,只需筛选出索引大于0的索引。好的,筛选结果解决了此问题。非常感谢!是的,指向一个键值并检查
isset()
也是个好主意。谢谢!
"(?'txt'(?<=:txt{)[^}]+(?=}))|(?'reg'(?<=:reg{)[^}]+(?=}))"
$matches = null;
$returnValue = preg_match_all('/(?\'txt\'(?<=:txt{)[^}]+(?=}))|(?\'reg\'(?<=:reg{)[^}]+(?=}))/', ':txt{resultxt}:txt{test}:reg{/^[a-zA-Z]*$/}:reg{regexresult}', $matches, PREG_OFFSET_CAPTURE);
array (
  0 => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
  'txt' => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => // No match found, index -1
    array (
      0 => '',
      1 => -1,
    ),
    3 => // No match found, index -1
    array (
      0 => '',
      1 => -1,
    ),
  ),
  1 => 
  array (
    0 => 
    array (
      0 => 'resultxt',
      1 => 5,
    ),
    1 => 
    array (
      0 => 'test',
      1 => 19,
    ),
    2 => 
    array (
      0 => '',
      1 => -1,
    ),
    3 => 
    array (
      0 => '',
      1 => -1,
    ),
  ),
  'reg' => 
  array (
    0 => '', // No match found, not array of 2 elements [<matched text>, <index>]
    1 => '', // No match found, not array of 2 elements [<matched text>, <index>]
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
  2 => 
  array (
    0 => '',
    1 => '',
    2 => 
    array (
      0 => '/^[a-zA-Z]*$/',
      1 => 29,
    ),
    3 => 
    array (
      0 => 'regexresult',
      1 => 48,
    ),
  ),
)