1

I have a hashes.xml on a location on the web. I want to parse it for the fields name='hash'><string>78235A2449BA7188CBF95F7DD2D40A36</string>, the file has many fields with this pattern (the MD5 hash is only an example, they differ in the XML document), I want to get them all and print to stdout. As far as I get is to fetch the first occurence and print it out then I'm stuck.

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
Dominik Honnef
  • 17,937
  • 7
  • 41
  • 43
bsteo
  • 1,738
  • 6
  • 34
  • 60

1 Answers1

0

you are looking for the scan method:

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

The result is:

["name='hash'><string>AAAAAA224</string>", "name='hash'><string>AAAAAA224</string>", "name='hash'><string>AAAAAA224</string>", "name='hash'><string>666</string>"]

You can see this post as well.

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Yup, thanks izomorphius, this works :) Just "m = a.scan(/name='hash'>[A-F0-9]+.*<\/string>/)" not "$m = a.scan(/name='hash'>[A-F0-9]+.*<\/string>/)" – bsteo Mar 04 '12 at 12:30
  • well in fact I used $m to be able to load the script in irb and see the contests of $m. Still for the example just m is enough. I will edit my post. – Ivaylo Strandjev Mar 04 '12 at 12:32