正则表达式用于匹配三字组

时间:2019-05-30 16:20:51

标签: php regex preg-match-all regex-negation regex-group

我正在尝试从一个字符串中获取所有三个单词的组-该字符串可以包含多个句子-而不会越过句子边界。我对只有标准字母的单词有效:

preg_match_all("/(?=(\b(\w+)(?:\s+(\w+)\b|$)(?:\s+(\w+)\b|$)))/",$utext,$matches);
print_r($matches[1]);

但是它掉在有撇号或连字符的地方。因此,使用以下示例文本:

The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.

我想要这个列表:

  • 咖啡色
  • 快速的棕狐狸
  • 棕狐狸的脚
  • 狐狸的脚跳了
  • 脚跳了过去
  • 跳过
  • 懒惰的
  • 懒狗
  • 下雨了
  • 雨水排在首位
  • 首屈一指
  • 领先
  • 平原

我尝试为每个\ w使用[\ w'-],但这有一些奇怪之处:<​​/ p>

Array ( [0] => The quick brown [1] => quick brown fox's [2] => brown fox's feet [3] => fox's feet jumped [4] => 's feet jumped [5] => s feet jumped [6] => feet jumped over [7] => jumped over the [8] => over the lazy [9] => the lazy dog [10] => The rain falls [11] => rain falls head-first [12] => falls head-first in [13] => head-first in the [14] => -first in the [15] => first in the [16] => in the plain )

我想念什么?谢谢。

1 个答案:

答案 0 :(得分:4)

只需将\w更改为[^\s.](而不是空格或点),然后删除单词boudaries。另一个更改是在正则表达式的开头添加一个交替的“行或空格的开头”:

$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain.";

preg_match_all("/(?=((?<=^|\s)[^\s.]+(?:\s+[^\s.]+|$)(?:\s+[^\s.]+|$)))/",$text,$matches);
print_r($matches[1]);

输出:

Array
(
    [0] => The quick brown
    [1] => quick brown fox's
    [2] => brown fox's feet
    [3] => fox's feet jumped
    [4] => feet jumped over
    [5] => jumped over the
    [6] => over the lazy
    [7] => the lazy dog
    [8] => The rain falls
    [9] => rain falls head-first
    [10] => falls head-first in
    [11] => head-first in the
    [12] => in the plain
)

正则表达式说明:

(?=                     # lookahead
    (                   # start group 1
        (?<=^|\s)       # lookbehind, make sure we have beginning of line or space before
        [^\s.]+         # 1 or more non space, non dot
        (?:             # non capture group
            \s+         # 1 or more spaces
            [^\s.]+     # 1 or more non space, non dot
          |             # OR
            $           # end of line
        )               # end group
        (?:             # non capture group
            \s+         # 1 or more spaces
            [^\s.]+     # 1 or more non space, non dot
          |             # OR
            $           # end of line
        )               # end group
    )                   # end group 1
)                       # end lookahead

根据评论进行编辑。

$text = "The quick brown fox's feet jumped over the lazy dog. The rain falls head-first in the plain. 'This is a quote,' I say, and that's that.";

preg_match_all("/(?=((?<=^|\s|')(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+(?:\s+(?:(?<=[a-zA-Z])'(?=[a-zA-Z])|[^\s.,'])+|$){2}))/",$text,$matches);
print_r($matches[1]);

输出:

Array
(
    [0] => The quick brown
    [1] => quick brown fox's
    [2] => brown fox's feet
    [3] => fox's feet jumped
    [4] => s feet jumped
    [5] => feet jumped over
    [6] => jumped over the
    [7] => over the lazy
    [8] => the lazy dog
    [9] => The rain falls
    [10] => rain falls head-first
    [11] => falls head-first in
    [12] => head-first in the
    [13] => in the plain
    [14] => This is a
    [15] => is a quote
    [16] => and that's that
)

正则表达式说明:

(?=                             # lookahead
    (                           # start group 1
        (?<=^|\s|')             # lookbehind, make sure we have beginning of line or space or quote before
        (?:                     # start non capture group
            (?<=[a-zA-Z])       # lookbehind, make sure we have a letter before
            '                   # a single quote
            (?=[a-zA-Z])        # lookahead, make sure we have a letter after
          |                     # OR
            [^\s.,']            # not a space or dot or comma or single quote
        )+                      # group may appear 1 or more times
        (?:                     # non capture group
            \s+                 # 1 or more spaces
            (?:                 # non capture group
                (?<=[a-zA-Z])   # lookbehind, make sure we have a letter before
                '               # a single quote
                (?=[a-zA-Z])    # lookahead, make sure we have a letter after
              |                 # OR
                [^\s.,']        # not a space or dot or comma or single quote
            )+                  # group may appear 1 or more times
          |                     # OR
            $                   # end of line
        ){2}                    # end group, must appear twice
    )                           # end group 1
)                               # end lookahead