Ruby中的正则表达式 - 提取Word

时间:2018-01-15 03:46:18

标签: r ruby regex

我已经在R中解决了以下文字。 有谁知道如何用Ruby编写它?

text <- 'have a nice day, @hello, mr burs'
x <- gsub('.*(@\\w+).*', '\\1', text)
x

[1] "@hello"

谢谢!

1 个答案:

答案 0 :(得分:1)

s = 'have a nice day, @hello, mr burs'
s =~ /.*(@\w+).*/   # match
$~[1]               # this will return "@hello"
# $~ means "the last regexp match", a `MatchData` instance. And we can get matched group by index.