首页 > 解决方案 > 如何使用正则表达式在一两个匹配组之后捕获句子的其余部分?

问题描述

所以我有两个正在使用的句子,我有兴趣根据单词中的字符制作特定的捕获组。所以我有这两个西班牙语句子:

  1. Yo quiero irme de viaje。
  2. Yo puedo caminar en la nieve。

第一个捕获组必须是其中一个动词,即。“quiero”和“puedo”所以我用这个正则表达式来做([PpDdQq].*o)
第二个捕获组必须是紧跟在动词之后的单词,以“me”结尾,我用(\w*me).
现在对于最后一个捕获组,在没有以“-me”结尾的直接单词的情况下,必须是紧跟在第一个捕获组之后的所有单词和空格,或者是紧跟在第二个捕获组之后的所有单词和空格。我使用了以“-me”结尾的直接词,(\w.+)但它不起作用。

谁能帮我弄清楚为什么?谢谢。以下是完整的正则表达式和正则表达式网站的链接,其中包含要匹配的表达式和示例:

([PpDdQq].*o) |(\w*me)|(\w.+)

标签: pythonregexre

解决方案


利用

\b([PpDdQq]\w*o)(?:\s+(\w*me))?\b(.*)

请参阅正则表达式证明

解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [PpDdQq]                 any character of: 'P', 'p', 'D', 'd',
                             'Q', 'q'
--------------------------------------------------------------------------------
    \w*                      word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    o                        'o'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (                        group and capture to \2:
--------------------------------------------------------------------------------
      \w*                      word characters (a-z, A-Z, 0-9, _) (0
                               or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      me                       'me'
--------------------------------------------------------------------------------
    )                        end of \2
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \3

推荐阅读