我正在使用Ruby 2.3。我如何写一个正则表达式,该表达式会在其前面找到一个空间或弦的开始的单词?我有这个字符串…
2.3.0 :001 > string = "time abcd”
=> "time abcd"
我可以写
2.3.0 :003 > string.index(/^time/)
=> 0
,但我想提出一个更通用的正则表达式,如果我的话在行的开头或它的前面有一个空间。
re = /
(?<= # just before this, match
^ # the start of the string
| # or
s # a single whitespace
) # and then
time # the literal string "time"
/x
# or equivalently: re = /(?<=^|s)time/
"time abcd".index(re)
# => 0