Ruby Regex gsub - 字符串后的所有内容

Alf*_*lfo 1 ruby regex gsub

我有一个类似的字符串:

test:awesome my search term with spaces
Run Code Online (Sandbox Code Playgroud)

我想立即将字符串提取test:到一个变量中,将其他所有内容提取到另一个变量中,所以我最终会awesome在一个变量中又my search term with spaces在另一个变量中。

从逻辑上讲,我要做的是将匹配的所有内容移动test:*到另一个变量中,然后删除第一个之前的所有内容:,留下我想要的内容。

目前我正在使用/test:(.*)([\s]+)/匹配第一部分,但我似乎无法正确获取第二部分。

Rob*_*t K 5

正则表达式中的第一个捕获是贪婪的,并且匹配空格,因为您使用了.. 而是尝试:

matches = string.match(/test:(\S*) (.*)/)
# index 0 is the whole pattern that was matched
first = matches[1] # this is the first () group
second = matches[2] # and the second () group
Run Code Online (Sandbox Code Playgroud)