0
my-test-string-4-23

From the above string how can i extract only string that is only my-test-string

I tries to_s method but it returns whole string as it is.

4

2 回答 2

1

仅对数字进行拆分,拆分返回一个数组,然后 gsub 删除最后一个“-”

str = "my-test-string-4-23"

str.split(/(\d+)/)[0].gsub(/\-$/, '')

将返回

"my-test-string" 
于 2013-10-14T16:36:52.157 回答
0

your_string.match('[\D-]')[0].[0..-1] 可能有效。它将返回非小数和破折号的第一个实例,然后删除最后一个 -

于 2013-10-14T16:37:35.837 回答