my_string = 'Here's the #: 49848! - but will dashes, commas & stars (*) show?'
puts src.gsub(/\d|\W/, "")
即可以刪除or(「|」)。刪除非gsub(/ d | W /,「」)的非字符的較短方法
以下是我到這裏的方式,我可以縮短嗎?
src = "Here's the #: 49848! - but will dashes, commas & stars (*) show?"
puts "A) - " + src
puts "B) - " + src.gsub(/\d\s?/, "")
puts "C) - " + src.gsub(/\W\s?/, "")
puts "D) - " + src.gsub(/\d|\W\s?/, "")
puts "E) - " + src.gsub(/\d|\W/, "")
puts "F) - " + src
A) - Here's the #: 49848! - but will dashes, commas & stars (*) show?
B) - Here's the #: ! - but will dashes, commas & stars (*) show?
C) - Heresthe49848butwilldashescommasstarsshow
D) - Heresthebutwilldashescommasstarsshow
E) - Heresthebutwilldashescommasstarsshow
F) - Here's the #: 49848! - but will dashes, commas & stars (*) show?
n.d. D)和E)是我想要的輸出。只是人物。
這已經很短了。你是否試圖在石頭上雕刻這些代碼? :-) – 2012-02-02 20:53:22