ruby - 删除某些字符之间的子字符串
问题描述
我想知道是否有办法从字符串中删除两个胡萝卜之间的所有内容:
"<@000000> I thought you said this would happen?"
要得到:
"I thought you said this would happen?"
我认为使用全局替换会起作用。我尝试使用此代码:
somefile = File.open("Test_Teleó.txt", "a+")
jdoc.fetch("messages").each do |body|
ts = body["ts"].to_i
somefile.puts body["text"].gsub(/[<]/, '').gsub(/[>]/, '').chomp
end
输出是:
@000000 I thought you said this would happen?
它只删除了胡萝卜而不是字符之间。
有什么建议吗?
解决方案
只需对您的代码稍作调整:
File.open("Test_Teleó.txt", "a+") do |somefile|
jdoc.fetch("messages").each do |body|
ts = body["ts"].to_i
somefile.puts body["text"].gsub(/<[^>]*>/, '').chomp
end
end
我在open
这里也使用了块样式,因为它会在块完成时自动关闭文件。这有助于避免忘记和意外打开文件。
推荐阅读
- asynchronous - async Elixir vs async Julia
- git - 清除 ucm 到 git
- angular - 我想在选择角度时删除一个选项
- angular - 选择选项时 Ng-select 集成测试失败
- python - 序列化器获取相关字段
- sonarqube - 声纳未能检测到主要问题“不应“注释掉代码部分”
- python - 无法下载带有希腊名称的文件
- php - 如何使用 MySqli 准备好的语句绑定由通配符包围的文本文字?
- spring - Wildfly - Java8 - UT010009(不实现 javax.servlet.Servlet)
- php - Vue/Axios CORS 成功但响应为空