0

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

我想拆分以下内容:

轨道 1:由%?

轨道 2:由 a 分隔和一个

然而,并非所有卡都有两条轨道,有些只有第一个,有些只有第二个。

如果存在,我想找到一个解析出 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
4

1 回答 1

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来找出正则表达式模式(免费站点,无从属关系)。

于 2019-03-21T15:56:44.920 回答