简体   繁体   English

ruby正则表达式,以gsub为字符串的特定部分

[英]ruby regular expression to gsub a particular portion of a string

How to gsub a sub-string that starts with a particular character or enclosed by particular characters in ruby? 如何gsub一个以特定字符starts withenclosed by ruby中的特定字符enclosed by的子字符串?

As for example :input is : Prime rates[ US Effective Date: 12/16/2008 ] I want to gsub the sub-string that is enclosed by two square braces [ and ] and want output like : Prime Rates 例如:输入为: Prime rates[ US Effective Date: 12/16/2008 ]我想gsub由两个方括号[和]括起来的子字符串,并希望输出如下: Prime Rates

How can I do that using ruby regular expression? 我怎么能用ruby正则表达式做到这一点?

Maybe something like: 也许是这样的:

s.gsub(/\[.*\]/, "")

This will remove anything enclosed in [] assuming there is no more than one occurrence of such substring. 这将删除[]包含的任何内容,假设此类子字符串不会出现多于一次。

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

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