0

I am trying to read pan numbers from physical credit cards using Vision Framework. For this I use a string extension for filtering the reuslts.

Here is an example for US phone numbers:

func extractPhoneNumber() -> (Range<String.Index>, String)? {
        let pattern = #"""
        (?x)                    # Verbose regex, allows comments
        (?:\+1-?)?              # Potential international prefix, may have -
        [(]?                    # Potential opening (
        \b(\w{3})               # Capture xxx
        [)]?                    # Potential closing )
        [\ -./]?                # Potential separator
        (\w{3})                 # Capture xxx
        [\ -./]?                # Potential separator
        (\w{4})\b               # Capture xxxx
        """#

        guard let range = self.range(of: pattern, options: .regularExpression, range: nil, locale: nil) else {
            // No phone number found.
            return nil
        }

I need a different pattern for Card Pan numbers in the following form eg. 0000 0000 0000 0000

Thank you for the help!

User123335511231
  • 11,654
  • 4
  • 18
  • 22
  • Should be quite easy? `\d{4} \d{4} \d{4} \d{4}` or if the whitespace maybe an issue: `\d{4}\s+\d{4}\s+\d{4}\s+\d{4}`. Did you try? i would imagine this has defo been asked and answered on here before now – Scriptable Nov 22 '19 at 14:21
  • Reference: https://stackoverflow.com/questions/9315647/regex-credit-card-number-tests – Scriptable Nov 22 '19 at 14:24
  • Don’t forget to apply the Luhn algorithm as well, once you have the numbers, to check that it is a valid credit card number. – Chris Nov 22 '19 at 14:31

1 Answers1

0

The pattern that worked for me is:

let pattern = #"""
        \d{4} \d{4} \d{4} \d{4}
        """#

Thank you Scriptable!

User123335511231
  • 11,654
  • 4
  • 18
  • 22