字符串修剪Ruby

时间:2014-02-18 18:15:32

标签: ruby string

我有以下字符串"survey_questions_attributes_1392746726560_question_title"我需要修剪从第一个字符开始直到第一个数字的所有字符串。怎么做?

5 个答案:

答案 0 :(得分:4)

String#slice方法支持正则表达式,因此您可以这样做:

[67] pry(main)> "survey_questions_attributes_1392746726560_question_title"[/\d.*/]
=> "1392746726560_question_title"

答案 1 :(得分:1)

"survey_questions_attributes_1392746726560_question_title".sub /\D*/,''

答案 2 :(得分:0)

我会使用String#[]

s = "survey_questions_attributes_1392746726560_question_title"
s[/[a-z_]+(?:\d)/] = ""
s # => "1392746726560_question_title"  

答案 3 :(得分:0)

看到Rubular的力量,并享受它的荣耀。

string =  "survey_questions_attributes_223233333_question_title"
puts string.sub( string.scan(/survey_questions_attributes_(.*)/)[0][0] , " ")

答案 4 :(得分:0)

"survey_questions_attributes_1392746726560_question_title".split(/\D+/, 2).last
# => "1392746726560_question_title"