patternrubyMinor
Getting the Twitter account from various input formats
Viewed 0 times
thegettingaccountinputformatstwitterfromvarious
Problem
I have an input for users to enter a Twitter account in any different way and I want to extract the user account.
For example:
The script that I've written to extract the data is the following:
It actually works, outputting the following:
But as I'm really newbie in Ruby I wanted to check for possible improvements.
For example:
twitters = [
"www.twitter.com/twitteruser1",
"@twitteruser2",
"twitteruser3",
"https://twitter.com/twitteruser4",
"https://www.twitter.com/twitteruser5",
"www.twitter.com/twitteruser6",
"http://www.twitter.com/twitteruser7",
"http://www.twitter.com/twitteruser8",
"twitter.com/twitteruser9"
]The script that I've written to extract the data is the following:
twitters.each do |twitter|
# for the url
twitter_user = twitter.match(/twitter.com\/([^\/.]*)$/)
if twitter_user != nil
puts twitter_user[1]
next
end
# for @ beginning
twitter_user = twitter.match(/^@([^\/.]*)$/)
if twitter_user != nil
puts twitter_user[1]
next
end
# if we arrive, we haven't found any coincidence
puts twitter
endIt actually works, outputting the following:
twitteruser1
twitteruser2
twitteruser3
twitteruser4
twitteruser5
twitteruser6
twitteruser7
twitteruser8
twitteruser9But as I'm really newbie in Ruby I wanted to check for possible improvements.
Solution
You can use a ruby case statement to check for different regex matches. If a match is found, it is by definition not nil, so it removes the need for a nil check as well.
When ruby does a regex match, any capture groups are assigned to the global variables
Note on your regex: If you want the capture groups to actually contain anything, you should change the asterisk to a plus:
twitters.each do |twitter|
case twitter
# for the url
when /twitter.com\/([^\/.]*)$/
puts $1
# for @ beginning
when /^@([^\/.]*)$/
puts $1
# if we arrive, we haven't found any coincidence
else
puts twitter
end
endWhen ruby does a regex match, any capture groups are assigned to the global variables
$1, $2, $3, and so on. See this question on StackOverflow for more details about the mechanic.Note on your regex: If you want the capture groups to actually contain anything, you should change the asterisk to a plus:
([^\/.]+). Without the plus, "www.twitter.com/" will be captured by one of the regex. With the plus, that string will fall into the default case.Code Snippets
twitters.each do |twitter|
case twitter
# for the url
when /twitter.com\/([^\/.]*)$/
puts $1
# for @ beginning
when /^@([^\/.]*)$/
puts $1
# if we arrive, we haven't found any coincidence
else
puts twitter
end
endContext
StackExchange Code Review Q#150001, answer score: 2
Revisions (0)
No revisions yet.