Need help to create regex to ignore spaces in card number.

Phurba Tshering Gurung 21 Reputation points


I am trying to setup DLP for card number. Card number gets blocked by default Microsoft DLP policy but the issue is when I type the card number in spacing format. Rules is not able to detect.

4660445675435678 - Detect and work
466044 5675 4356 78 - Does not detect
4660 445675 435478 - Does not detect

I mean spacing format might be in any format, there is no any specific spacing format. User may try to send in any spacing format.

I just need here is to ignore the spaces used in card number. So, that it will only match card number regardless of any spacing format used
in between.

Please anyone help me for creating the regex which fulfills the requirements.

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,377 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,497 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,831 Reputation points

    This is a pretty naive attempt at meeting those requirements. The regex will match a series of digits from 15 to 19 digits (the length of credit card numbers varies between providers (ref: Payment_card_number) and it makes no attempt to verify that the card number is actually a credit card number and not, say, some sort of inventory identifier, part number, or other long(ish) string of digits. The regex tries to match the longest string first and then shorter and shorter strings of digits. It may produce false positives if there are non-space characters used to separate the digits. You should do your own testing before relying on it!

    $regex = "(?:x)(?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d)|   #19 digits  
                   (?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d)|       #18 digits  
                   (?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d)|           #17 digits  
                   (?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d)|               #16 digits  
                   (?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d)"                   #15 digits  
    "46604 4567543567",          #15  
    "4660 445 67 5435 67",  
    "4 6 6044567543 56 7",  
    "4660445 675 43567",  
    "4660 44 567543567",   
    "4660x44 567543567" |  
            if ($_ -match "\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d ?\d"){  
                Write-Host "I'm a match: $_" -ForegroundColor Green  
                Write-Host "I'm not a match: $_" -ForegroundColor Yellow  

    If there is possibility of finding multiple spaces between the digits you'll have to modify the regex. Here's a brief example of that:

    "1 2  3   4" -match "\d ? *?\d ? *?\d ? .*?\d"  

0 additional answers

Sort by: Most helpful