Regex 正则表达式:最多匹配第三个空格
我有以下几句话:Regex 正则表达式:最多匹配第三个空格,regex,Regex,我有以下几句话: Data 5 in:out:40 Files 我想匹配所有字符串直到第三个空格,所以,在本例中,我想返回 Data 5 in:out:40 那么: ^(\S+\s+\S+\s+\S+) 让我们将其分解: ^ # start from string beginning ( # match everything inside (begin) \S+ # match all non-whitespace(s) \s+ # whitespace(s)
Data 5 in:out:40 Files
我想匹配所有字符串直到第三个空格,所以,在本例中,我想返回
Data 5 in:out:40
那么:
^(\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)
你可以。到目前为止你试过什么?让我们看一些代码:-)为什么在正则表达式中有一个与数字毫无关系的
\d
?我想他那里有数字。如果看不到更多的输入示例,就说不出什么。当他对自己的数据更具体时,可以很容易地修改模式。只需将[^\s]
和\d
替换为\s
,就可以完全按照OP所描述的方式进行操作。然后,您可以提出自己的答案。OP可以选择最适合他的。