如何在ruby中使用gsub regex [英] How to use gsub regex in ruby
本文介绍了如何在ruby中使用gsub regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从红宝石正则表达式中删除字符串的一部分:
I want to remove some part of string from using ruby regex:
value =本地主机:8393/foobar/1测试:foobartest 我想从字符串[localhost:8393/foobar/1 test:foobartest]和其余的值中删除"test",以便输出看起来像这样:
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!
推荐答案
我会做这样的事情:
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"
这篇关于如何在ruby中使用gsub regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文