lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 18 Dec 2012, at 12:55, ranier@cultura.com.br wrote:

> Anybody can help with pattern to process http://www.bcb.gov.br/rex/ftp/paises.txt

The file is fixed width, so the second field has a lot of spaces I assume you don't want to capture?

The below works on Lua 5.1. On 5.2 you can replace the %a%d%p with %g

Thanks,
Kev

local parsed = {}
for line in io.lines() do
	local num, val = line:match("^(%d+) ([%a%d%p ]+[%a%d%p])%s+$")
	if num == nil then
		error(string.format("Unable to parse: '%s'", line))
	end

	local t = {}
	t.num = num
	t.val = val

	parsed[#parsed+1] = t
end

for i, t in ipairs(parsed) do
	print(string.format("%d: '%s' '%s'", i, t.num, t.val))
end