如何使用正则表达式替换部分字符串

时间:2011-07-06 14:30:35

标签: ruby regex str-replace

我想替换所有:

xmlns="http://xml.blablabla"

 xmlns:something="xxxx"

表示字符串中的空格。

由于

3 个答案:

答案 0 :(得分:2)

str.sub!(/xmlns=.+?"/, ' ')

答案 1 :(得分:1)

String#sub是你的朋友:

my_string = '<Fare_MasterPricerCalendarReply xmlns="http://xml.blablabla">'
my_string.sub(/\s.+$/, " >")
# => "<Fare_MasterPricerCalendarReply >"

答案 2 :(得分:0)

使用此正则表达式:xmlns="[^"]*",它匹配xmlns="http://xml.blablabla",然后将匹配替换为空字符串。

相关问题