Home > front end >  Regex Track1 Magnetic Stripe
Regex Track1 Magnetic Stripe

Time:07-16

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

  • Allowed Characters would be: ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 .()-$
  • Not Allowed Characters would be: ^!"&'* ,:;<=>@_[]#%?
  • Required character - only must character / appear
  • The max size is 26 characters , min size 2.

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:

  • ^TEST/TEST^- Should Pass
  • ^TEST TEST^- Should Fail
  • ^TEST/TE/ST^ - Should Fail
  • ^TEST/TE ST^ - Should Fail

Thanks!

CodePudding user response:

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

  • \^ Match ^
  • (?=[A-Z .()\/-]{1,24}\^) Positive lookahead, assert 1,24 of the allowed chars followed by ^ to the right to make a total of 2-26 characters
  • [A-Z .()-]*\/[A-Z .()-]* Match / between optional allowed chars
  • \^ Match ^

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

  • Related