I have a string h2#test- ruby is so awesome, blah, blah, blah
. I want to have #test
to be the only characters left. is th开发者_JAVA技巧ere a way in ruby to run a Regular Expression that removes all other characters?
str.match(/(\#\w+)/)[1]
Don't think of it as removing all other characters...think of it as capturing what you want.
'h2#test- ruby is so awesome, blah, blah, blah' =~ /(#test)/
your_captured_text = $1
str[/#[a-z]+/i]
精彩评论