简体   繁体   English

如何在ruby中使用gsub regex

[英]How to use gsub regex in ruby

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: 值=本地主机:8393 / foobar / 1测试:foobartest我想从字符串[本地主机:8393 / foobar / 1测试:foobartest]和其余的值中删除“测试”,以便输出看起来像:

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! 谢谢!

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM