0

A Card reader is simply a keyboard input that once a Card is swiped, a string is displayed in any focused text field.

I would like to split the following:

Track 1: which is delimited by a % and a ?

Track 2: which is delimited by a ; and a ?

However not all Cards have both Tracks, some have only the first, and some have only the second.

I'd like to find a RegEx that parses out Track 1 and Track 2 if either exist.

Here are sample Cards swipes which generates these Strings:

%12345?;54321?               (has both Track 1 & Track 2)
%1234678?                    (has only Track 1)
;98765?                      (has only Track 2)
%93857563932746584?;38475?   (has both Track 1 & Track 2)

This is the example I'm building off of:

%([0-9]+)\?) // for first Track
;([0-9]+)\?) // for second Track
Yozef
  • 829
  • 11
  • 27
  • While you wait to find the RegEx... For such simple strings, why not just use `substr` and/or `substring` together with `firstIndexOf(x)` or `lastIndexOf(x)` where the `x` could be the `%`or `?` or `;`... Also if your "tracks" are numerical why not extract by splitting at & then also removing special characters (leaves tracks)? – VC.One Mar 21 '19 at 14:45
  • PS: I think you got down-voted because your programming question has neither RegEx nor AS3 codes. Show your best try with this regEx and maybe someone can fix it. – VC.One Mar 21 '19 at 14:48
  • I did use finding by Index the delimeters, and substring-ing the indexes, however it did not work for all my use cases. – Yozef Mar 21 '19 at 20:52

1 Answers1

2

This regex will match your track groupings:

/(?:%([0-9]+)\?)?(?:;([0-9]+)\?)?/g

(?:            // non-capturing group
    %          // match the % character
    (          // capturing group for the first number
        [0-9]  // match digits; could also use \d
        +      // match 1 or more digits
    )          // close the group
    \?         // match the ? character
)
?              // match 0 or 1 of the non-capturing group
(?:
    ;          // match the ; character
        [0-9]  
        +
    )
    \?
)
?

BTW, I used regexr to figure out the regex patterns here (free site, no affiliation).

Brian
  • 3,850
  • 3
  • 21
  • 37