【问题标题】:How to set the pattern for Credit Card PAN NUMBER with regularExpresion in Swift?如何在 Swift 中使用正则表达式设置信用卡 PAN NUMBER 的模式?
【发布时间】:2020-03-18 14:30:25
【问题描述】:

我正在尝试使用 Vision Framework 从实体信用卡中读取泛号。为此,我使用字符串扩展名来过滤 reuslts。

以下是美国电话号码的示例:

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
        }

我需要以下形式的卡片盘编号的不同模式,例如。 0000 0000 0000 0000

感谢您的帮助!

【问题讨论】:

  • 应该很容易吧? \d{4} \d{4} \d{4} \d{4} 或者如果空格可能是一个问题:\d{4}\s+\d{4}\s+\d{4}\s+\d{4}。你试过了吗?我想在此之前已经在这里询问和回答了这个问题
  • 别忘了应用 Luhn 算法,一旦你有数字,检查它是否是一个有效的信用卡号码。

标签: ios swift credit-card banking


【解决方案1】:

对我有用的模式是:

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

感谢 Scriptable!

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 2023-03-17
    • 2016-07-31
    • 2010-11-30
    • 2015-09-14
    • 2018-08-15
    • 2012-11-10
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多