[英]replace a string with a specfic format using regex - Ruby
This is my code below. 这是我的下面的代码。 I have some 100 text files and i want to replace numerous strings in a specific format which includes a value respective to a key in a hash map.
我有大约100个文本文件,我想替换特定格式的许多字符串,其中包括与哈希图中的键对应的值。
You can understand the scenario if you have a look at the below code. 如果您看下面的代码,就可以理解这种情况。
require 'yaml'
map = YAML.load(File.open("/Documents/final.yml"))
Dir.glob('/Documents/**/*.txt') do |item|
next if item == '.' or item == '..'
contents = File.read(item)
newFile = File.open(item, "w")
contents = contents.gsub(/My data\(@"([^"]+)",([^,]+),([^,]+),([^,]+)\)/) {'Your data(@'+ $1 +','+ $2 +',' + $3 + ',' + map[$1] + ',' + $4 + ')'}
newFile.write(contents)
end
for some reason i get an error as below [ test2.rb is my ruby script file] 由于某种原因,我得到如下错误[test2.rb是我的ruby脚本文件]
test2.rb:9:in `+': no implicit conversion of nil into String (TypeError)
from test2.rb:9:in `block (2 levels) in <main>'
from test2.rb:9:in `gsub'
from test2.rb:9:in `block in <main>'
from test2.rb:5:in `glob'
from test2.rb:5:in `<main>'
If i remove map[$1] from the code, i get the output properly. 如果我从代码中删除map [$ 1],我将正确获得输出。
contents = contents.gsub(/My data\(@"([^"]+)",([^,]+),([^,]+),([^,]+)\)/) {'Your data(@'+ $1 +','+ $2 +',' + $3 + ',' + $4 + ')'}
Also if i remove the other strings and have only map[$1], i still get the expected output. 另外,如果我删除其他字符串并且只有map [$ 1],我仍然会得到预期的输出。
contents = contents.gsub(/My data\(@"([^"]+)",([^,]+),([^,]+),([^,]+)\)/) { map[$1] }
But together it doesnot work. 但是在一起却不起作用。
Any help will be appreciated 任何帮助将不胜感激
To expand on @NeilSlater's answer, this would be one possible clean solution: 为了扩展@NeilSlater的答案,这将是一种可能的干净解决方案:
contents.gsub(/My data\(@"([^"]+)",([^,]+),([^,]+),([^,]+)\)/) do
values = [$1, $2, $3, map[$1], $4].join(',')
"Your data(@#{values})"
end
in this case, join
will implicitly call to_s
on the array contents. 在这种情况下,
join
将隐式调用数组内容中的to_s
。 However, that means that if a value is not found in map
, you will end up with an empty value, eg 但是,这意味着,如果在
map
找不到值,则最终将得到一个空值,例如
Your data(@123,a,b,,c)
您的数据(@ 123,a,b ,, c)
if you want to omit this, call compact
on the array before joining it and you'll end up with 如果您想忽略它,请在加入数组之前在数组上调用
compact
,最后得到
Your data(@123,a,b,c)
您的数据(@ 123,a,b,c)
The value of map[$1]
is nil
for at least one entry in one of the files. 对于其中一个文件中的至少一个条目,
map[$1]
值为nil
。 On its own, this can be stored in an Array and output as you require. 它可以单独存储在数组中,并根据需要输出。 When concatenated using
+
it causes the error that you see. 使用
+
串联时,将导致您看到的错误。
You can force a conversion to String by using map[$1].to_s
instead. 您可以改为使用
map[$1].to_s
强制转换为String。 For nil
this will return the empty String ""
, which is probably what you want. 对于
nil
它将返回空字符串""
,这可能是您想要的。
You could also use Ruby's built-in string interpolation which does the same thing internally and is a good match to your use - the block after .gsub
could be { "Your data(@#{$1},#{$2},#{$3},#{map[$1]},#{$4})" }
您还可以使用Ruby的内置字符串插值法,该方法在内部做相同的事情,并且与您的用法非常匹配
.gsub
之后的.gsub
块可以是{ "Your data(@#{$1},#{$2},#{$3},#{map[$1]},#{$4})" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.