从字符串中提取所有带有@符号的单词

Extract all words with @ symbol from a string

我需要使用 rails/ruby:

从一个字符串(对于 Twitter)中提取所有@usernames
String Examples:
"@tom @john how are you?"
"how are you @john?"
"@tom hi"

该函数应该从字符串中提取所有用户名,加上用户名不允许使用的特殊字符...如您所见“?”在一个例子中...

有多种方法可以做到 - 这里是一种方法:

string = "@tom @john how are you?"
words = string.split " "
twitter_handles = words.select do |word|
  word.start_with?('@') && word[1..-1].chars.all? do |char|
    char =~ /[a-zA-Z1-9\_]/
  end && word.length > 1
end

char =~ 正则表达式只接受字母数字和下划线

r = /
    @              # match character
    [[[:alpha:]]]+ # match one or more letters
    \b             # match word break
    /x             # free-spacing regex definition mode

"@tom @john how are you? And you, @andré?".scan(r)
  #=> ["@tom", "@john", "@andré"]

如果您想 return

 ["tom", "john", "andré"]

将正则表达式的第一行从 @ 更改为

(?<=@)

这是一个正面回顾。它要求字符 "@" 存在,但它不会成为匹配项的一部分。

来自“Why can't I register certain usernames?”:

A username can only contain alphanumeric characters (letters A-Z, numbers 0-9) with the exception of underscores, as noted above. Check to make sure your desired username doesn't contain any symbols, dashes, or spaces.

\w metacharacter is equivalent to [a-zA-Z0-9_]:

/\w/ - A word character ([a-zA-Z0-9_])

简单扫描@\w+就成功了,根据:

strings = [
  "@tom @john how are you?",
  "how are you @john?",
  "@tom hi",
  "@foo @_foo @foo_ @foo_bar @f123bar @f_123_bar"
]

strings.map { |s| s.scan(/@\w+/) }
# => [["@tom", "@john"],
#     ["@john"],
#     ["@tom"],
#     ["@foo", "@_foo", "@foo_", "@foo_bar", "@f123bar", "@f_123_bar"]]