Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Regex 类似正则表达式的gmail搜索操作符-PHP preg_match_Regex_Search_Gmail - Fatal编程技术网

Regex 类似正则表达式的gmail搜索操作符-PHP preg_match

Regex 类似正则表达式的gmail搜索操作符-PHP preg_match,regex,search,gmail,Regex,Search,Gmail,我正在尝试实现一个类似于gmail搜索操作符的系统,使用PHP中的函数preg_match分割输入字符串。 例如: 输入字符串=>command1:word1 word2 command2:word3 command3:word4 wordN 输出数组=>( 命令1:word1-word2, 命令2:word3, 命令3:word4 wordN ) 下面的帖子解释了如何做到这一点: 我已经用preg_match测试了它,但不匹配。我认为正则表达式可能会因系统而异。 猜猜PHP中的正则表达式如何匹

我正在尝试实现一个类似于gmail搜索操作符的系统,使用PHP中的函数preg_match分割输入字符串。 例如:

输入字符串=>command1:word1 word2 command2:word3 command3:word4 wordN
输出数组=>(
命令1:word1-word2,
命令2:word3,
命令3:word4 wordN
)

下面的帖子解释了如何做到这一点:

我已经用preg_match测试了它,但不匹配。我认为正则表达式可能会因系统而异。
猜猜PHP中的正则表达式如何匹配这个问题

preg_match('/\s+(?=\w+:)/i','command1:word1 word2 command2:word3 command3:word4 wordN',$test); 

谢谢,

您可以使用以下内容:

<?php
$input = 'command1:word1 word2 command2:word3 command3:word4 wordN command1:word3';
preg_match_all('/
  (?:
    ([^: ]+) # command
    : # trailing ":"
  )
  (
    [^: ]+  # 1st word
    (?:\s+[^: ]+\b(?!:))* # possible other words, starts with spaces, does not end with ":"
  )
  /x', $input, $matches, PREG_SET_ORDER);

$result = array();
foreach ($matches as $match) {
  $result[$match[1]] = $result[$match[1]] ? $result[$match[1]] . ' ' . $match[2] : $match[2];
}

var_dump($result);

pre_split而不是preg_match会做得很好实际上(至少今天是2016-07-27)当标准中有特殊字符时,gmail会添加括号:
到:(testing@test.test)主题:(测试)来源:test
很酷!但这段代码给出了一条“PHP通知:未定义索引”消息。。。要解决此问题,只需使用isset($result[$match[1]])。。。相反,.@bruno.braga是的,isset可能会解决这个问题。最后的正则表达式是
'/(?:([^:]+):([^:]+(?:\s+[^:]+\b(?!:)*)/”
对我来说很好,谢谢