-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledger_regex.py
64 lines (59 loc) · 2.25 KB
/
ledger_regex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Defines all patterns to find transactions and relevant information.
@author: Etienne Monier <[email protected]>
@license: CC-BY-NC-SA
@since: 2021-02-01
"""
# flake8: noqa: E501
# Pattern to catch automatic transactions.
#
# It catches the following groups:
# 1. The account regex
# 2. The postings lines
pattern_autom = r"""
(?<=\n)=\ /([^/]+)/[ \t]*\n # REGEX definition
((?:\ [ \t]*
(?:[^;#\%\|\*\n\t ]|(?<!\ )\ )+[^;#\%\|\*\n\t ] # ACCOUNT
(?:[ {2}\t][ \t]* # HARD SEP
[$£¥€¢]? # CURRENCY SYMB
-?\d+(?:,\d{3})*(?:\.\d{1,2})? # NUMBER
(?:\ [A-Za-z]+)? # CURRENCY NAME
(?:\ \"[^"]+\")? # CURRENCY NAME LONG
)?
[ \t]*\n)+)
"""
# Pattern to catch posting information.
#
# It catches the following groups:
# 1. The account name
# 2. The currency symbol
# 3. The amount
# 4. The currency name
# 5. The long currency name
posting_pattern = r"""
^\ [ \t]*
((?:[^;#\%\|\*\n\t ]|(?<!\ )\ )+[^;#\%\|\*\n\t ])# ACCOUNT
(?:[ {2}\t][ \t]* # HARD SEP
([$£¥€¢]?) # CURRENCY SYMB
(-?\d+(?:,\d{3})*(?:\.\d{1,2})?) # NUMBER
((?:\ [A-Za-z]+)?) # CURRENCY NAME
((?:\ \"[^"]+\")?) # CURRENCY NAME LONG
)?
.*$
"""
# Pattern to catch a whole user transaction.
user_trans_pattern = r"^((?:\d{2}[/-]\d{2}[/-]\d{4})|(?:\d{4}[/-]\d{2}[/-]\d{2})).+\n([ \t]+.+\n)+"
# Pattern to catch the first line of a user transaction.
#
# It catches the following groups:
# 1. The date
# 2. The payee
trans_date_line = r"""
^
((?:\d{2}[/-]\d{2}[/-]\d{4}) | (?:\d{4}[/-]\d{2}[/-]\d{2})) # DATE
(?:=(?:\d{2}[/-]\d{2}[/-]\d{4}) | (?:\d{4}[/-]\d{2}[/-]\d{2}))? # AUX DATE
[ \t]+ # SEP
(?:[!\*][ \t]+)? # STATE
((?:[^;#\%\|\*\n\t ]|(?<!\ )\ )+[^;#\%\|\*\n\t ]) # PAYEE
.*$ # Commentary and co.
"""