convert: keep original description by default, allow backreferences in replace pattern

This commit is contained in:
Simon Michael 2010-04-01 21:04:28 +00:00
parent b34015475b
commit c2cf9b2185
2 changed files with 13 additions and 12 deletions

View File

@ -13,7 +13,7 @@ import Ledger.Amount (nullmixedamt)
import System.IO (stderr)
import Text.CSV (parseCSVFromFile, printCSV)
import Text.Printf (hPrintf)
import Text.RegexPR (matchRegexPR)
import Text.RegexPR (matchRegexPR, gsubRegexPR)
import Data.Maybe
import Ledger.Dates (firstJust, showDate, parsedate)
import System.Locale (defaultTimeLocale)
@ -307,12 +307,11 @@ identify rules defacct desc | null matchingrules = (defacct,desc)
| otherwise = (acct,newdesc)
where
matchingrules = filter ismatch rules :: [AccountRule]
where ismatch = any (isJust . flip matchregex desc . fst) . fst
where ismatch = any (isJust . flip matchRegexPR (caseinsensitive desc) . fst) . fst
(prs,acct) = head matchingrules
mrs = filter (isJust . fst) $ map (\(p,r) -> (matchregex p desc, r)) prs
(m,repl) = head mrs
matched = fst $ fst $ fromJust m
newdesc = fromMaybe matched repl
matchregex = matchRegexPR . ("(?i)" ++)
p_ms_r = filter (\(_,m,_) -> isJust m) $ map (\(p,r) -> (p, matchRegexPR (caseinsensitive p) desc, r)) prs
(p,_,r) = head p_ms_r
newdesc = case r of Just rpat -> gsubRegexPR (caseinsensitive p) rpat desc
Nothing -> desc
caseinsensitive = ("(?i)"++)

10
MANUAL
View File

@ -436,10 +436,12 @@ Notes:
account name to use when the transaction's description matches any of
these patterns.
- A match pattern may optionally be followed by = and a replacement
pattern, which will become the ledger transaction's description.
Otherwise the matched part of the csv description is used. (To preserve
the full csv description, use .* before and after the match pattern.)
- A match pattern may be followed by a replacement pattern, separated by
``=``, which rewrites the matched part of the description. Use this if
you want to clean up messy bank data. To rewrite the entire description,
use a match pattern like ``.*PAT.*=REPL``. Within a replacement pattern,
you can refer to the matched text with ``\0`` and any regex groups
with ``\1``, ``\2`` in the usual way.
test
''''