正则表达式:匹配第3个空格

时间:2018-06-05 10:41:29

标签: regex

我有以下几行:

Data  5 in:out:40 Files 

我希望匹配所有字符串直到第3个空格,所以,在这种情况下,我想要回来

Data  5 in:out:40

1 个答案:

答案 0 :(得分:0)

怎么样:

^(\S+\s+\S+\s+\S+)

让我们打破这个:

^ # start from string beginning
( # match everything inside (begin)
  \S+     # match all non-whitespace(s)
  \s+     # whitespace(s)
  \d+     # match all non-whitespace(s)
  \s+     # whitespace(s)
  \S+     # match all non-whitespace(s)
) # match everything inside (end)

你可以test the regex in a debugger