开发者

Ruby - Remove all other characters from a String

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜