问题
I want to remove some part of string from using ruby regex:
value = localhost:8393/foobar/1 test:foobartest I want to remove "test" from my string [localhost:8393/foobar/1 test:foobartest] and rest of the value so that output should look like:
localhost:8393/foobar/1
How to do this in ruby? Can you share some sample code to achieve this?
Appreciated your help in advance! Thanks!
回答1:
I would do something like this:
value = 'localhost:8393/foobar/1 test:foobartest'
value.split.first
#=> "localhost:8393/foobar/1"
Or if you want to use an regexp:
value.sub(/ test.*/, '')
"localhost:8393/foobar/1"
来源:https://stackoverflow.com/questions/29269937/how-to-use-gsub-regex-in-ruby