我有以下邏輯Ruby的正則表達式代碼字符串
def insert_type(type_list, user_input)
case user_input
when user_input == 'library_1'
type_list.slice!('library_' + RegEx for any digit after library_ and stop right there + '__')
type_list << user_input << '__'
when user_input == 'class_A_2'
type_list.slice!('class_' + RegEx for any digit after class_ and stop right there + _A + '__')
type_list << user_input << '__'
end
end
我試着做以下
[l][i][b][r][a][r][y][_]\d{0,5} #digit from 0 to 99999
它的工作,但應該有一個更傳統的方式在那裏,我可以開始用l和下劃線結尾,然後添加數字,因爲type_list可能是:
puts type_list
=> "username_John__userid_58173__userpass_8adsh20__class_A_2__library_0__"
我真的不明白你想要做什麼。你不能只是'__'(雙下劃線)? – nhahtdh
看起來像這是你正在尋找'\ w + \ d {0,5}' – thefourtheye
哦,讓我試試看。 – user3163916