如何在 ruby 中使用 gsub 正则表达式

How to use gsub regex in ruby

我想使用 ruby 正则表达式删除部分字符串:

值=localhost:8393/foobar/1test:foobartest 我想从我的字符串 [localhost:8393/foobar/1 test:foobartest] 和其余值中删除 "test" 以便输出看起来像:

localhost:8393/foobar/1

如何在 ruby 中执行此操作?你能分享一些示例代码来实现这个吗?

提前感谢您的帮助! 谢谢!

我会这样做:

value = 'localhost:8393/foobar/1 test:foobartest'

value.split.first
#=> "localhost:8393/foobar/1"

或者如果您想使用正则表达式:

value.sub(/ test.*/, '')
"localhost:8393/foobar/1"