Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions ledger-regex.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,36 @@
(require 'cl-lib)

(defconst ledger-amount-regex
(concat "\\( \\|\t\\| \t\\)[ \t]*-?"
"\\([A-Z$€£₹_(]+ *\\)?"
;; We either match just a number after the commodity with no
;; decimal or thousand separators or a number with thousand
;; separators. If we have a decimal part starting with `,'
;; or `.', because the match is non-greedy, it must leave at
;; least one of those symbols for the following capture
;; group, which then finishes the decimal part.
"\\(-?\\(?:[0-9]+\\|[0-9,.]+?\\)\\)"
"\\([,.][0-9)]+\\)?"
"\\( *[[:word:]€£₹_\"]+\\)?"
"\\([ \t]*[@={]@?[^\n;]+?\\)?"
"\\([ \t]+;.+?\\|[ \t]*\\)?$"))

(let ((prefix-commodity-re "[A-Z$€£₹_(]+ *")
(suffix-commodity-re " *[[:word:]€£₹_\"]+")
(value-re "-?\\(?:[0-9]+\\|[0-9,.]+?\\)")
(decimal-re "[,.][0-9)]+"))
(concat "\\( \\|\t\\| \t\\)[ \t]*-?"
"(?[ \t]*"

;; If it's an expression pick the last value.
"\\(?:"
"\\(?:" prefix-commodity-re "\\)?"
"\\(?:" value-re "\\)"
"\\(?:" decimal-re "\\)?"
"\\(?:" suffix-commodity-re "\\)?"
"\\(?:[ \t]*[+*/-][ \t]*\\)?"
"\\)*?"

"\\(" prefix-commodity-re "\\)?"
;; We either match just a number after the commodity with no
;; decimal or thousand separators or a number with thousand
;; separators. If we have a decimal part starting with `,'
;; or `.', because the match is non-greedy, it must leave at
;; least one of those symbols for the following capture
;; group, which then finishes the decimal part.
"\\(" value-re "\\)"
"\\(" decimal-re "\\)?"
"\\(" suffix-commodity-re "\\)?"
"[ \t]*)?"
"\\([ \t]*[@={]@?[^\n;]+?\\)?"
"\\([ \t]+;.+?\\|[ \t]*\\)?$")))

(defconst ledger-amount-decimal-comma-regex
"-?[1-9][0-9.]*[,]?[0-9]*")
Expand Down