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

10
MANUAL
View File

@ -436,10 +436,12 @@ Notes:
account name to use when the transaction's description matches any of account name to use when the transaction's description matches any of
these patterns. these patterns.
- A match pattern may optionally be followed by = and a replacement - A match pattern may be followed by a replacement pattern, separated by
pattern, which will become the ledger transaction's description. ``=``, which rewrites the matched part of the description. Use this if
Otherwise the matched part of the csv description is used. (To preserve you want to clean up messy bank data. To rewrite the entire description,
the full csv description, use .* before and after the match pattern.) 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 test
'''' ''''