我有一个像这样的字符串
test = "row-even row-dd testing"
现在我想只得到以row-
开头的单词。
我用过test =〜/ year-(。*)/ 但它一直让我回归0.anyidea我怎么能这样做?
答案 0 :(得分:2)
r = /
(?<=\A|\s) # match start of string or space in a positive lookbehind
row- # match string 'row-'
[[:alpha:]]+ # match one or more letters
(?=\s|\z) # match a space or end of string in a positive lookahead
/x # free-spacing regex definition mode
"row-even row-dd testing".scan(r) #=> ["row-even", "row-dd"]
"borrow-even row-dd testing".scan(r) #=> ["row-dd"]
"row-even row-dd1 testing".scan(r) #=> ["row-even"]
答案 1 :(得分:1)
0是找到的模式的起始位置。
请改为尝试:
puts test.scan(/row-\w+/)