RegEX - 查找Track 1和Track 2子串的磁卡字符串

问题描述 投票:0回答:1

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

我想分开以下内容:

轨道1:由%和a分隔?

第2轨:由a分隔;和一个?

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

我想找到一个RegEx来解析Track 1和Track 2(如果存在的话)。

以下是生成这些字符串的示例卡片滑动:

%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
regex actionscript-3 substring magnetic-cards
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来弄清楚这里的正则表达式模式(免费网站,没有隶属关系)。

© www.soinside.com 2019 - 2024. All rights reserved.