[英]Ruby - Remove all other characters from a String
I have a string h2#test- ruby is so awesome, blah, blah, blah
. 我有一个字符串h2#test- ruby is so awesome, blah, blah, blah
。 I want to have #test
to be the only characters left. 我想让#test
成为剩下的唯一字符。 is there a way in ruby to run a Regular Expression that removes all other characters? ruby中有没有一种方法可以运行删除所有其他字符的正则表达式?
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.