我在网络上的某个位置有一个hashes.xml。我想为字段name='hash'><string>78235A2449BA7188CBF95F7DD2D40A36</string>
解析它,该文件有许多具有此模式的字段(MD5哈希只是一个示例,它们在XML文档中有所不同),我想将它们全部打印并打印到stdout。我得到的是取出第一个出现并将其打印出来然后我就卡住了。
for locale in (locales)
while hash.nil? do
headers = {
'Host' => server,
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => locale.length.to_s,
}
resp, data = http.post(path, locale, headers)
# Extract the hash
data =~ /name='hash'\>\<string\>([A-F0-9]+).*\<\/string\>/m
hash = $1
mylocale = locale
break if hash.nil?
end
end
if hash.nil?
puts "ERROR"
exit(1)
end
puts "Hash: "+hash
答案 0 :(得分:0)
您正在寻找扫描方法:
a =<<END
this is some example name='hash'><string>AAAAAA224</string>
name='hash'><string>AAAAAA224</string>
vname='hash'><string>AAAAAA224</string>
example for you name='hash'><string>666</string>
END
m = a.scan(/name='hash'><string>[A-F0-9]+.*<\/string>/)
puts m.inspect
结果是:
["name='hash'><string>AAAAAA224</string>", "name='hash'><string>AAAAAA224</string>", "name='hash'><string>AAAAAA224</string>", "name='hash'><string>666</string>"]
您也可以看到this帖子。