Skip to content

Instantly share code, notes, and snippets.

@alvinncx
Last active April 20, 2019 06:37
Show Gist options
  • Save alvinncx/a6878a9dc23845b687222cf62e265d3b to your computer and use it in GitHub Desktop.
Save alvinncx/a6878a9dc23845b687222cf62e265d3b to your computer and use it in GitHub Desktop.
Parsing DBS transactions
defmodule Parser do
@moduledoc """
Sample parser for data from DBS. It is annoying that there isn't a csv from DBS bank
Sample data:
18 Mar 2019 GRAB *16236905-9-139 S$23.00
18 Mar 2019 GRAB *2B3B63422351042EE S$10.00
18 Mar 2019 GRAB *1391705-9-138 S$15.00
19 Mar 2019 2C2P - ZARA.COM SG S$29.90 cr
19 Mar 2019 2C2P - ZARA.COM SG S$79.80
21 Mar 2019 STARBUCKS COFFEE SINGA S$50.00
"""
@regex_figures ~r/(S\$)(\d{0,}\.\d{0,2})/
@doc """
Parses file
"""
def parse() do
File.read!("./sample")
|> String.split("\n")
|> Enum.map(&(&1 |> parse_line!))
end
def parse_line!(line) do
case <<date::binary-size(11), rest::binary>> = line do
line ->
{item, amount, type} = rest |> parse_rest
%{
date: date,
item: item,
amount: amount,
type: type
}
_ ->
{:error}
end
end
def parse_rest(line) do
case Regex.split(@regex_figures, line, include_captures: :true, trim: true) do
[line, figures] ->
{line |> String.trim, figures, "debit"}
[line, figures, " cr"] ->
{line |> String.trim, figures, "credit"}
_rest ->
{:error}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment