quentinsf / icsv2ledger

Interactive importing of CSV files to Ledger
196 stars 70 forks source link

Add more extensive regexp possibilities #121

Open tinloaf opened 5 years ago

tinloaf commented 5 years ago

Hi,

first things first:

Now for the actual change: For my daily use, the functionality the current mappings provide is not sufficient. That's because my bank has one large "description" field which contains payee, description, sometimes the payee's payment info, and a reference number. What I need is a set of regular expressions that dissect the various formats that this field can come in and set payee, comment, tags etc. accordingly.

I thought about extending the current mappings file, but that didn't feel like the best solution for two reasons:

So, I added another "regexp file", which is expected to be in JSON format. From the documentation of the read_regex_file function:


The regex file is a json file, consisting of a list of objects, each object containing:

The syntax of an entry in the file is:

    {"trigger":
         {"where": <index of the column against which to match>,
          "regex": "<regular expression>",
          "search": true/false (if true, use re.search, otherwise use re.match)},
     "action": {…}
    }

There are currently four different possible actions:

    {"type": "set-payee",
     "value": "<new payee>"}
    {"type": "set-tag",
     "value": "<which tag to set>"}
    {"type": "set-account",
     "value": "<which account to set>"}
    {"type": "set-addon",
     "name": "<name of the addon field>",
     "value": "<value to set>"}

If the regular expression contains any named groups, those named groups will be used to format the strings given for value. Thus, an entry like:

    {"trigger":
        {"where": 2,
         "regex": "The payee is (?<payee>.*)"},
     "action":
        {"type": "set-payee",
         "value": "{payee}"}
    }

would set the payee of any transaction that has the string The payee is somebody in the second column to somebody.