Ok this is rather embarrassing but I have this string:
>> t1
=> ["name: Big Lebowski\n"]
Then I want to replace the entire line with ""
>> t2 = t1.collect{|n| n.gsub("/^name.*$/", "")}
=> ["name: Big Lebowski\n"]
I get the same thing. What gives?
You have put your regular expression inside a string, which obviously won't work.
>> t2 = t1.collect{|n| n.gsub(/^name.*$/, "")}
=> ["\n"]
If you also want to get rid of the newline, use the m
regex modifier.
>> t2 = t1.collect{|n| n.gsub(/^name.*$/m, "")}
=> [""]
That's because you're using "gsub" in you block instead of "gsub!" - as the last one modifies the target. Try:
t2 = t1.collect{|n| n.gsub!("/^name.*$/", "")}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.