首页 > 解决方案 > 使用正则表达式从具有已知前缀的字符串中提取多个单词

问题描述

我有以下输入

 Obvious directions are:
  west, east, southeast, south.

...并希望得到以下输出:

west east southeast south

为我做这件事的神奇正则表达式是什么?

我已经提取了第一个标记,west所以我认为我需要一个乘数/递归/某物或其他东西,但我一生都找不到什么。

(?<= Obvious directions are:\s+)(\w+)(?=[,\.])

标签: regex

解决方案


采用

(?<=Obvious directions are:[^.]*?)\w+

证明

解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    Obvious directions       'Obvious directions are:'
    are:
--------------------------------------------------------------------------------
    [^.]*?                   any character except: '.' (0 or more
                             times (matching the least amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))

推荐阅读