我从文件中导入行。在此行中,一个(转义的)字符串是该行的一部分:
DP,0,"021",257
DP,1,"022",257
DP,2,"023",513
DP,3,"024",513
DP,4,"025",1025
DP,5,"026",1025
DP,6,"081",257
DP,7,"082",257
DP,8,"083",513
DP,9,"084",513
DP,10,"085",1025
DP,11,"086",1025
DP,12,"087",1025
DP,13,"091",257
DP,14,"092",513
DP,15,"093",1025
IS,0,"FIX",0
IS,1,"KARIN02",0
IS,2,"KARUIT02",0
IS,3,"KARIN02HOV",0
IS,4,"KARUIT02HOV",0
IS,5,"KARIN08",0
IS,6,"KARUIT08",0
IS,7,"KARIN08HOV",0
IS,8,"KARUIT08HOV",0
IS,9,"KARIN09",0
IS,10,"KARUIT09",0
IS,11,"KARIN09HOV",0
IS,12,"KARUIT09HOV",0
IS,13,"KARIN10",0
IS,14,"KARUIT10",0
IS,15,"KARIN10HOV",0
我得到以下对象(如果是DP):
index - parts1 (int)
name - parts2 (string)
ref - parts3 (int)
我尝试使用REGEX替换行中的原义序列,但没有效果
@name_to_ID = {}
kruising = 2007
File.open(cfgFile).each{|line|
parts = line.split(",")
if parts[0]=="DP"
index = parts[1].to_i
hex = index.to_s(16).upcase.rjust(2, '0')
cname = parts[2].to_s
tname = cname.gsub('\\"','')
p "cname= #{cname} (#{cname.length})"
p "tname= #{tname} (#{tname.length})"
p cname == tname
@name_to_ID[tname] = kruising.to_s + "-" + hex.to_s
end
}
teststring = "021"
p @name_to_ID[teststring]
> "021" (5)
> "021" (5)
> true
> nil
从另一个字符串引用(length3)调用时,问题暴露出来
hash [key]不等于字符串“ 021”(长度5)不是字符串021(长度3)
实际上可以替换我需要的字符的任何方法吗?
编辑:我用过
cname.each_char{|c|
p c
}
> "\""
> "0"
> "2"
> "1"
> "\""
编辑:请求的结果更新:
# Current output:
@name_to_ID["021"] = 2007-00 "021".length = 5
@name_to_ID["022"] = 2007-01 "022".length = 5
@name_to_ID["081"] = 2007-06 "081".length = 5
@name_to_ID["082"] = 2007-07 "082".length = 5
@name_to_ID["091"] = 2007-0D "091".length = 5
@name_to_ID["101"] = 2007-10 "101".length = 5
# -------------
# Expected output:
@name_to_ID["021"] = 2007-00 "021".length = 3
@name_to_ID["022"] = 2007-01 "022".length = 3
@name_to_ID["081"] = 2007-06 "081".length = 3
@name_to_ID["082"] = 2007-07 "082".length = 3
@name_to_ID["091"] = 2007-0D "091".length = 3
@name_to_ID["101"] = 2007-10 "101".length = 3
答案 0 :(得分:0)
您的问题是您不知道字符串中的正确字符。打印时可能不是相同的字符。
尝试parts[2].to_s.bytes
检查该意外字符的确切字符代码。例如:
> "asd".bytes
=> [205, 184, 97, 115, 100]
或者,如果您确定字符串的每个部分都具有相同的格式,则可以删除第一个和最后一个字符:
cname = parts[2].to_s[1..-2]
或者,如果您知道字符串将不包含任何特殊字符,则可以删除字符串中的所有特殊字符
cname = parts[2].to_s.gsub(/[^0-9A-Za-z]/, '')