From 5c85a1dd1c5348c98eef0674378dff619dd211c4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 22 Sep 2017 16:43:03 -1000 Subject: [PATCH] lib: allow trailing whitespace in regex account aliases Trailing whitespace in the replacement part of a regular expression account alias is now significant. Eg, slightly flattening some bank accounts: --alias '/:somebank:/=somebank ' --- hledger-lib/Hledger/Read/JournalReader.hs | 4 ++-- hledger-lib/doc/hledger_journal.5.m4.md | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index e8a2f6c8e..a41539bfc 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -321,7 +321,7 @@ basicaliasp = do old <- rstrip <$> (some $ noneOf ("=" :: [Char])) char '=' many spacenonewline - new <- rstrip <$> anyChar `manyTill` eolof -- don't require a final newline, good for cli options + new <- rstrip <$> anyChar `manyTill` eolof -- eol in journal, eof in command lines, normally return $ BasicAlias (T.pack old) (T.pack new) regexaliasp :: TextParser m AccountAlias @@ -333,7 +333,7 @@ regexaliasp = do many spacenonewline char '=' many spacenonewline - repl <- rstrip <$> anyChar `manyTill` eolof + repl <- anyChar `manyTill` eolof return $ RegexAlias re repl endaliasesdirectivep :: JournalParser m () diff --git a/hledger-lib/doc/hledger_journal.5.m4.md b/hledger-lib/doc/hledger_journal.5.m4.md index add41566f..56b8e52d8 100644 --- a/hledger-lib/doc/hledger_journal.5.m4.md +++ b/hledger-lib/doc/hledger_journal.5.m4.md @@ -672,8 +672,6 @@ inside an account name, the matched part will be replaced by REPLACEMENT. If REGEX contains parenthesised match groups, these can be referenced by the usual numeric backreferences in REPLACEMENT. -Note, currently regular expression aliases may cause noticeable slow-downs. -(And if you use Ledger on your hledger file, they will be ignored.) Eg: ```journal @@ -681,6 +679,9 @@ alias /^(.+):bank:([^:]+)(.*)/ = \1:\2 \3 # rewrites "assets:bank:wells fargo:checking" to "assets:wells fargo checking" ``` +Also note that REPLACEMENT continues to the end of line (or on command line, +to end of option argument), so it can contain trailing whitespace. + #### Multiple aliases You can define as many aliases as you like using directives or command-line options.