![](/img/trans.png)
[英]Ruby how to use regex to replace a part of a string and an additional part that is optional
[英]How to replace part of string using regex
我想替换所有:
xmlns="http://xml.blablabla"
和
xmlns:something="xxxx"
用于字符串中的空格。
谢谢
str.sub!(/xmlns=.+?"/, ' ')
String#sub是你的朋友:
my_string = '<Fare_MasterPricerCalendarReply xmlns="http://xml.blablabla">'
my_string.sub(/\s.+$/, " >")
# => "<Fare_MasterPricerCalendarReply >"
使用这个正则表达式: xmlns="[^"]*"
,它匹配xmlns="http://xml.blablabla"
,然后用空字符串替换匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.