首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文本中检测IBAN

在文本中检测IBAN
EN

Stack Overflow用户
提问于 2016-11-15 17:17:36
回答 2查看 8.2K关注 0票数 1

有一个简单的regex模式可以检测文本中的IBAN (包括特定的国家格式?)

实际上,我正在用这个正则表达式找到IBAN:

代码语言:javascript
复制
[a-zA-Z]{2}[0-9]{2}[0-9a-zA-Z]{10,30}

但这与格式化的IBAN类似(正常,我没有引入白空间检测)不匹配:

代码语言:javascript
复制
FR76 30003 02420 002202XXXXX 77

代码语言:javascript
复制
PT50 0002 0123 1234 5678 9015 4

你能帮我吗?我在哪里可以找到所有按国家格式的IBAN模式?

例子:

“我的IBAN是PT50 0002 0123 1234 5678 9015 1,用正则表达式捕捉它,这个PT50000201231234567890151也是!”

我想提取/处理"PT50 0002 0123 1234 5678 9015 1“和"PT50000201231234567890151”。

编辑:解决方案1-非常长的模式:

代码语言:javascript
复制
((NO)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(NO)[0-9A-Z]{13}|(BE)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}|(BE)[0-9A-Z]{14}|(DK|FO|FI|GL|NL)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{2}|(DK|FO|FI|GL|NL)[0-9A-Z]{16}|(MK|SI)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(MK|SI)[0-9A-Z]{17}|(BA|EE|KZ|LT|LU|AT)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}|(BA|EE|KZ|LT|LU|AT)[0-9A-Z]{18}|(HR|LI|LV|CH)[0-9A-Z]{2}[ ][0-9A-Z]{4}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|LB|PL)[0-9A-Z]{26}|(MU)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{2}|(MU)[0-9A-Z]{28}|(MT)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(MT)[0-9A-Z]{29})

也不适用于法国的特定模式。

EN

回答 2

Stack Overflow用户

发布于 2016-11-15 17:22:49

使用的Regex对有效的IBANs是不正确的。用这个Regex代替

代码语言:javascript
复制
 [a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}

来源:http://snipplr.com/view/15322/iban-regex-all-ibans/

有关IBAN格式的更多信息:

Number

编辑:

对于更复杂的验证器,请检查以下代码https://jsfiddle.net/kf332bhj/1/

若要处理空格,请检测国家并根据字符长度定义正则表达式。

查看SWIFT的最新IBAN标准

https://www.swift.com/standards

https://www.swift.com/standards/data-standards/iban

代码语言:javascript
复制
var CODE_LENGTHS = {
            AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29,
            CH: 21, CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24,
            FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21,
            HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28,
            LI: 21, LT: 20, LU: 20, LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27,
            MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
            RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26
        };

编辑2:

要回答Ingo,请查看https://www.swift.com/standards/data-standards/iban中的IBAN注册表(PDF)

( a)挪威的最低BBAN为11

( b)对于IBANs,9-15只需为数字(0-9{7})。例如,KZ86 125K ZT50 0410 0100对哈萨克斯坦有效。

票数 4
EN

Stack Overflow用户

发布于 2022-05-31 22:26:59

将解决方案分为两个步骤是合理的:

  1. 在课文中找出嫌疑人;
  2. 解析并(可选)验证每个嫌疑人。

以下是这种方法的实施情况。我们需要每个国家的IBAN长度清单。在我的例子中,国家列表非常有限,所以我在这里使用@Sully的答案中的一个。尽管越来越多的国家可能加入进来,但我们强烈建议获得最新的名单。

代码语言:javascript
复制
const CODE_LENGTHS = {
    AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29,
    CH: 21, CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24,
    FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21,
    HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28,
    LI: 21, LT: 20, LU: 20, LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27,
    MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
    RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26
};

// We use a regexp to find the suspects.
// IBAN starts from a 2-character country code followed by the checksum.
// So 4 symbols + the country specific number.
// The longest IBAN in this case is 31 symbols
// so the IBAN 'tail' is 31 - 4 = 27 characters. 
const MAX_LENGTH = Math.max(...Object.values(CODE_LENGTHS)) - 4;

// Let's say we want to highlight all the IBANs in the text.
const HighlightIBAN = text => text.replace(
    // Here is the magic. We search 2 letters followed by 2 digits
    // and followed by 27 meaningful characters.
    // The group (?:\W*[a-z0-9]) allows to ignore spaces, dots,
    // dashes or whatever a user may put as number group separators.
    new RegExp('[a-z]{2}[0-9]{2}(?:\\W*[a-z0-9]){0,' + MAX_LENGTH + '}', 'ig'),
    suspect => {
        // Here we have our suspect.
        // Just need to check if the IBAN is a IBAN indeed.
        const country = suspect.substr(0, 2).toUpperCase();
        const length = CODE_LENGTHS[country];

        // If the first 2 letters isn't a country code (not in the list)
        // we ignore the string found by the regex
        if(!length) return suspect;

        // Now we check if the finding contains the exact number
        // of meaningful symbols and extract the IBAN
        // using the same approach to ignore formatting
        const checkRegexp = new RegExp('^((?:\\W*[a-z0-9]){' + length + '})(.*)$', 'i');
        const parts = checkRegexp.exec(suspect);

        // Nothing extracted. The suitable sequence of symbols not found.
        // So we ignore the suspect again.
        if(!parts) return suspect;
        const iban = parts[1];
        const tail = parts[2];

        // We have our IBAN here.
        // The 'tail' is the symbols which follow the IBAN.
        // As we always pick 31 characters the string found
        // will have several symbols which shouldn't be touched at the end.

        // The extracted IBAN can be validated here (e.g. checksum and stuff)
        // like @Sully suggested.
        // Then we do our job. E.g. we can highlight the IBAN found.
        // Just don't forget to put the 'tail' back.
        return `<span class="iban">${iban}</span>${tail}`;
    }
)

在这里可以找到示例用法:https://jsfiddle.net/qkwx5ja7/1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40615902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档