【问题标题】:Matching operator:term and operator: term with PHP search operators [closed]匹配运算符:术语和运算符:带有 PHP 搜索运算符的术语 [关闭]
【发布时间】:2013-08-01 20:35:01
【问题描述】:

我正在使用我在this question 上找到的答案来允许我拥有搜索运算符。

它的一个问题是它要求 then 运算符和单词之间没有空格。

所以它将匹配 operator:something 但不匹配 operator: something 所以我想知道如何匹配任何一种形式?

preg_match_all('/
  (?:
    ([^: ]+) # command
    : # trailing ":"
  )
  (
    [^: ]+  # 1st word
    (?:\s+[^: ]+\b(?!:))* # possible other words, starts with spaces, does not end with ":"
  )
  /x',
  $search, $matches, PREG_SET_ORDER);

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

【问题讨论】:

  • 您在 Savannah 提供的代码中所做的更改,您只是在问答案而没有对此进行处理
  • 您必须发布输入字符串和预期的输出,而不是指向不良书面帖子的链接。谢谢。

标签: php regex search operators preg-match-all


【解决方案1】:
:\s*

\s 是空格的标志,* 表示 0 或更多

【讨论】:

    【解决方案2】:

    第四行

    :\ ? # trailing ":" or ": "
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      相关资源
      最近更新 更多