regexregex-groupmagnetic-cards

Regex Track1 Magnetic Stripe


How can I create regex rule for name in Track1, following these rules:

I tried:

\^[^\^!"&'*+,:;<=>@_\[\]\\#%?]{2,26}\^ Result FAIL: removing "/" will pass pattern 
\^([-.()0-9a-zA-Z]*\/[-.()\w\s\/]*){1,26}\^ Result FAIL: more than 26 characters will pass pattern
^[-.()\w\s\/]{2,26}\^ Result FAIL: removing "/" will pass pattern

Sample of Name in Track1:

Thanks!


Solution

  • If there has to be at least ^ at the start and end, and there has to be at least a single / then the minimum amount of characters would be 3 instead of 2.

    In that case, you might use:

    \^(?=[A-Z .()\/-]{1,24}\^)[A-Z .()-]*\/[A-Z .()-]*\^
    

    Explanation

    See a regex demo.

    If the / can not be at the start or at the end (matching at least 5 characters in that case)

    \^(?=[A-Z .()\/-]{1,24}\^)[A-Z .()-]+\/[A-Z .()-]+\^
    

    See another regex demo