簡體   English   中英

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

[英]RegEX - Find Magnetic Card String for Track 1 and Track 2 substrings

讀卡器只是鍵盤輸入,一旦刷卡,就會在任何焦點文本字段中顯示一個字符串。

我想拆分以下內容:

音軌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

此正則表達式將匹配您的曲目分組:

/(?:%([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找出這里的regex模式(免費站點,沒有從屬關系)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM