RegEX-查找磁道1和磁道2子串的磁卡

时间:2019-03-21 13:49:55

标签: regex actionscript-3 substring magnetic-cards

读卡器只是键盘输入,一旦刷卡,就会在任何焦点文本字段中显示一个字符串。

我想分割以下内容:

轨道1:由

分隔

轨道2:由;

分隔

但是,并非所有卡都具有两个轨道,有的仅具有第一个,而有的仅具有第二个。

我想找到一个可以解析出Track 1和Track 2的RegEx。

以下是刷卡示例,可生成以下字符串:

%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)

这是我要构建的示例:

%([0-9]+)\?) // for first Track
;([0-9]+)\?) // for second Track

1 个答案:

答案 0 :(得分:2)

此正则表达式将与您的曲目分组匹配:

/(?:%([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]  
        +
    )
    \?
)
?

顺便说一句,我用regexr来计算这里的正则表达式模式(免费站点,没有从属关系)。

相关问题