-
Notifications
You must be signed in to change notification settings - Fork 0
/
IBANBICFilter.py
executable file
·56 lines (41 loc) · 1.71 KB
/
IBANBICFilter.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
#!/usr/bin/python3
# pip3 install schwifty
import re
import schwifty
class IBANBICFilter():
def filter(self, line):
resulting_line = line
match = re.search('".*IBAN: (.*) BIC: ((\\w\\s?){11}|(\\w\\s?){8})"', resulting_line)
if not match is None and len(match.groups()) >= 2:
# replace bic
span = match.span(2)
index = span[0]
end_index = span[1]
try:
# raises exception if BIC can not be parsed
bic = schwifty.BIC(match.group(2))
# replace BIC string with compact string from successfully parsed BIC object
resulting_line = resulting_line[:index] + bic.compact + resulting_line[end_index:]
except:
pass
# replace iban
index = match.span(1)[0]
# try to parse iban of variable length - can be up to 32 dependening on the country code
# worst case a space after each character -> check lengths up to 64
for length in range(2, 64+1):
try:
# raises exception if IBAN can not be parsed
iban = schwifty.IBAN(resulting_line[index:index+length])
# replace IBAN string with compact string from successfully parsed IBAN object
resulting_line = resulting_line[:index] + iban.compact + resulting_line[index+length:]
break
except:
pass
return resulting_line
if __name__ == '__main__':
ibanBICFilter = IBANBICFilter()
while True:
try:
print(ibanBICFilter.filter(input()))
except EOFError:
break