branch: elpa/beancount
commit 4cae6eebb83f7232579974215c48e05e7f684b70
Author: Daniele Nicolodi <dani...@grinta.net>
Commit: Daniele Nicolodi <dani...@grinta.net>

    beancount.el: Make beancount-number-regexp more strict
    
    Add a test for the regular expression and rewrite
    beancount-date-regexp to be sligtly more readable.
---
 beancount-tests.el | 17 +++++++++++++++++
 beancount.el       |  4 ++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/beancount-tests.el b/beancount-tests.el
index 6bdd2edea3..f0dc935db8 100644
--- a/beancount-tests.el
+++ b/beancount-tests.el
@@ -24,6 +24,23 @@
     (beancount-mode)
     (font-lock-ensure)))
 
+(ert-deftest beancount/number-regexp-001 ()
+  :tags '(regexp regress)
+  (let ((r (concat "\\`" beancount-number-regexp "\\'")))
+    (should (string-match r "1"))
+    (should (string-match r "1."))
+    (should (string-match r "1.0"))
+    (should (string-match r "10000.00"))
+    (should (string-match r "1,234.56"))
+    (should (string-match r "10,000.00"))
+    (should (string-match r "1,000,000.00"))
+    (should-not (string-match r "1.00.00"))
+    (should-not (string-match r "1,00.00"))
+    (should-not (string-match r ",000.00"))
+    (should-not (string-match r ".00"))
+    (should-not (string-match r "."))
+    (should-not (string-match r ","))))
+
 (defun beancount-test-fontify-string (string)
   "Fontify STRING in beancount-mode."
   (with-temp-buffer
diff --git a/beancount.el b/beancount.el
index f6dce380eb..74ca51fa5a 100644
--- a/beancount.el
+++ b/beancount.el
@@ -188,7 +188,7 @@ to align all amounts."
     "render_commas"
     "title"))
 
-(defconst beancount-date-regexp 
"[0-9][0-9][0-9][0-9][-/][0-9][0-9][-/][0-9][0-9]"
+(defconst beancount-date-regexp "[0-9]\\{4\\}[-/][0-9]\\{2\\}[-/][0-9]\\{2\\}"
   "A regular expression to match dates.")
 
 (defconst beancount-account-regexp
@@ -196,7 +196,7 @@ to align all amounts."
           "\\(?::[[:upper:]][[:alnum:]-_]+\\)+")
   "A regular expression to match account names.")
 
-(defconst beancount-number-regexp "[-+]?[0-9,]+\\(?:\\.[0-9]*\\)?"
+(defconst beancount-number-regexp 
"[-+]?[0-9]+\\(?:,[0-9]\\{3\\}\\)*\\(?:\\.[0-9]*\\)?"
   "A regular expression to match decimal numbers.")
 
 (defconst beancount-currency-regexp "[A-Z][A-Z-_'.]*"

Reply via email to