Skip to content

Commit

Permalink
Improve IBAN recognition on device
Browse files Browse the repository at this point in the history
PP-106
  • Loading branch information
igor-gini committed Feb 14, 2025
1 parent bca4d95 commit 5bdfece
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@

import Foundation

func extractIBANS(string: String) -> [String] {
var results = [String]()
var prefferedIBANs = [String]()
private func extractIBANMatches(from string: String) -> [NSTextCheckingResult] {
let stringRange = NSRange(location: 0, length: string.count)
let allIBANs = IBANKnowledge().universalIBANRegex.matches(in: string, options: [], range: stringRange)
let iBANsInBlocks = IBANKnowledge().ibanInBlocksRegex.matches(in: string, options: [], range: stringRange)
let matches = allIBANs + iBANsInBlocks
return allIBANs + iBANsInBlocks
}

func extractIBANS(string: String) -> [String] {
var results = [String]()
var prefferedIBANs = [String]()
var matches = extractIBANMatches(from: string)

// try to fix invalid strings
if matches.isEmpty, string.count > 2 {
var number = String(string.dropFirst(2))

number = number.replacingOccurrences(of: "s", with: "5")
number = number.replacingOccurrences(of: "S", with: "5")
number = number.replacingOccurrences(of: "I", with: "1")
number = number.replacingOccurrences(of: "i", with: "1")
number = number.replacingOccurrences(of: "l", with: "1")
number = number.replacingOccurrences(of: "T", with: "1")
number = number.replacingOccurrences(of: "o", with: "0")
number = number.replacingOccurrences(of: "O", with: "0")
number = number.replacingOccurrences(of: "Q", with: "0")
number = number.trimmingCharacters(in: .whitespaces)
number = number.replacingOccurrences(of: ",", with: "")
number = number.replacingOccurrences(of: ".", with: "")
matches = extractIBANMatches(from: String(string.prefix(2)) + number)
}

for match in matches {
if let range = Range(match.range, in: string) {
Expand Down

0 comments on commit 5bdfece

Please sign in to comment.