更新时间:2023-02-23 09:33:42
def link_urls_and_users s
#regexps
url = /( |^)http:\/\/([^\s]*\.[^\s]*)( |$)/
user = /@(\w+)/
#replace @usernames with links to that user
while s =~ user
s.sub! "@#{$1}", "<a href='http://twitter.com/#{$1}' >#{$1}</a>"
end
#replace urls with links
while s =~ url
name = $2
s.sub! /( |^)http:\/\/#{name}( |$)/, " <a href='http://#{name}' >#{name}</a> "
end
s
end
puts link_urls_and_users(tweet.text)
只要网址以空格填充或位于推文的开头和/或结尾,此方法就有效.
This works, so long as URLs are padded by spaces or are at the beginning and/or end of the tweet.