【发布时间】:2015-09-12 02:59:08
【问题描述】:
我有一个简单的小字符串“Wed 09 Sept”,我想选择“09 Sept”
这将从第一个空格中取出所有内容:
\s(.*)(来自Regex to get everything after the first space)
但它带有空间,我想摆脱它
【问题讨论】:
我有一个简单的小字符串“Wed 09 Sept”,我想选择“09 Sept”
这将从第一个空格中取出所有内容:
\s(.*)(来自Regex to get everything after the first space)
但它带有空间,我想摆脱它
【问题讨论】:
获取该正则表达式的第一个捕获组:
"Wed 09 Sept" =~ /\s(.*)/
selection = $1
# => "09 Sept"
=~
运算符将模式与字符串匹配。 $1、$2、$3等变量指的是捕获组。
【讨论】:
selection[/\s(.*)/][$1]
将第一个空格和其余的/(\s)(.*)/ 分组,例如:
pry(main)> m = /(?'first'\s)(?'rest'.*)/.match 'Wed 09 Sept'
=> #<MatchData " 09 Sept" first:" " rest:"09 Sept">
pry(main)> m[:rest]
=> "09 Sept"
【讨论】: