Skip to content

CSV: match group references get mixed up when multiple ifs match a record #2158

@amoralesch

Description

@amoralesch

When reading CSV and rules files and using REGEX expressions, if more than one if block matches the same record, the REGEX group index gets mixup.

Example:

  • CSV file:
date,desc
2019-01-01,SUFFIX First
2019-02-01,SUFFIX Text 1 - Text 2
  • Rules file:
skip          1

fields        date,desc
separator     ,
date-format   %Y-%m-%d
description   Test

if %desc SUFFIX (.*)
  account1      account:\1
  amount1       1
  account2      equity

if %desc SUFFIX (.*) - (.*)
  account1      account:\1:\2
  amount1       1
  account2      equity

Expected Result

2019-01-01 Test
    account:First               1
    equity

2019-02-01 Test
    account:Text 1:Text 2               1
    equity

Current Result

2019-01-01 Test
    account:First               1
    equity

2019-02-01 Test
    account:Text 1 - Text 2:Text 1               1
    equity

Note

If the rules file is modified with the following:

if %desc SUFFIX (.*) - (.*)
  account1      account:\2:\3
  amount1       1
  account2      equity

(note the index for matching groups (\2 and \3), then the result works as expected.

More info

HLedger version: hledger 1.32.2, mac-aarch64

[Background: wish #2009, pr #2087.
Workaround: don't allow multiple ifs with capture groups to match the same CSV record. So if you use capture groups, make those regexps specific enough that at most one of them will match any given CSV record.]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-BUGSomething wrong, confusing or sub-standard in the software, docs, or user experience.affects4-manyAffects potentially a significant number of users.annoyance2-minorMinor to moderate usability/doc bug, reasonably easy to avoid or tolerate.csvThe csv file format, csv output format, or generally CSV-related.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions