How to use gsub regex in ruby

若如初见. 提交于 2019-12-12 01:52:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!