簡體   English   中英

構造非結構文本文件的正則表達式

[英]Regular expressions to structure a non structure texte file

我是正則表達式的新手,我需要從以下文件中提取一些數據:

Card number: 604861*********3997, SEQ: 175
Current session ID: 175
21/01/2021 08:46:49 : Selected language : ENG
21/01/2021 08:46:49 : Chip application : NO Active 
 21/01/2021 09:53:03 : Returned code : 0
 21/01/2021 09:53:03 : Pin entered
 21/01/2021 09:53:09 : Transaction selected: FastCash 40000 on ATM: Y6652114
 21/01/2021 09:53:10 : Returned code : 0
21/01/2021 09:53:13  : FastCash Authorization requested
21/01/2021 09:53:14  : FastCash Authorized
Returned code : 0, STAN: 686817
21/01/2021 09:53:27  : Cash dispensed
21/01/2021 09:53:28  : Card ejected
21/01/2021 09:53:29  : Card taken
21/01/2021 09:53:32  : Dispense details:
->cass 1: 0 x 5000 MGA, cass 2: 4 x 10000 MGA
->cass 3: 0 x 10000 MGA, cass 4: 0 x 20000 MGA
------->Total cash dispensed: 40000 MGA
21/01/2021 09:53:32  : Cash presented
21/01/2021 09:53:32  : Waiting for cash to be taken
21/01/2021 09:53:33  : Cash taken
21/01/2021 09:53:39  : Transaction completed
 21/01/2021 09:53:41 : Session terminated
 Card number: 604861*********3997, SEQ: 185

這是我嘗試使用組捕獲的代碼,但它不適用於某些情況,它適用於其他情況,而且每次我嘗試組合這些表達式時,我都會得到“沒有重復錯誤”

const CARD_NUMBER = /number:\s*([\d*]*)/gi;
const TRANSACTION_AMOUNT = /FastCash\s*([\d,.]*)/gi;
const TRANSACTION_CURRENCY = /Total cash dispensed:\s*(\D)*/gi;
const TERMINAL_ID = /ATM:\s*(\w*)/;
const EXTERNAL_STAN = /STAN:\s*(\d*)/i;
const TRANSACTION_DATE = /([ a-zA-Z0-9/:_-]+)? : Session terminated/i;

// or combined 
var rx = new RegExp(
  "number:s(d+*+d+).*?FastCashs(d+).*?ATM:s(w+).*?STAN:s(d+).*?completed[\r\n]+(.*?)s)",
  "gi"
);
var matches = new Array();
while ((match = rx.exec(sample)) !== null) {
  matches.push(match);
}
console.log(matches);

所以這是我的正則表達式列表。

組合的正則表達式非常復雜,它使用了很多非捕獲組,它必須對結果進行 map 以刪除未定義的匹配組

 const str = document.body.innerText; document.body.innerText = ''; const CARD_NUMBER = /Card number:\s*(\d{19})/gi; const TRANSACTION_AMOUNT = /FastCash\s*(\d+)/gi; const TRANSACTION_CURRENCY = /Total cash dispensed:\s*(\d+)/gi; const TERMINAL_ID = /ATM:\s*(\w+)/gi; const EXTERNAL_STAN = /STAN:\s*(\d+)/gi; const TRANSACTION_DATE = /((?:\d{2,4}\/*)+\s*(?:\d{2}:*)+)\s*:\s*Session terminated/gi; const COMBINED = /Card number:\s*([\d\*]{19})|FastCash\s*(\d+)|Total cash dispensed:\s*(\d+)|ATM:\s*(\w+)|STAN:\s*(\d+)|((?:\d{2,4}\/*)+\s*(?:\d{2}:*)+)\s*:\s*Session terminated/gi; console.log([...str.matchAll(CARD_NUMBER)][0][1]); console.log([...str.matchAll(TRANSACTION_AMOUNT)][0][1]); console.log([...str.matchAll(TRANSACTION_CURRENCY)][0][1]); console.log([...str.matchAll(TERMINAL_ID)][0][1]); console.log([...str.matchAll(EXTERNAL_STAN)][0][1]); console.log([...str.matchAll(TRANSACTION_DATE)][0][1]); console.log('Combined'); console.log([...str.matchAll(COMBINED)].map(match => match.filter(a => a)[1]));
 Card number: 6048616048616048611, SEQ: 175 Current session ID: 175 21/01/2021 08:46:49: Selected language: ENG 21/01/2021 08:46:49: Chip application: NO Active 21/01/2021 09:53:03: Returned code: 0 21/01/2021 09:53:03: Pin entered 21/01/2021 09:53:09: Transaction selected: FastCash 40000 on ATM: Y6652114 21/01/2021 09:53:10: Returned code: 0 21/01/2021 09:53:13: FastCash Authorization requested 21/01/2021 09:53:14: FastCash Authorized Returned code: 0, STAN: 686817 21/01/2021 09:53:27: Cash dispensed 21/01/2021 09:53:28: Card ejected 21/01/2021 09:53:29: Card taken 21/01/2021 09:53:32: Dispense details: ->cass 1: 0 x 5000 MGA, cass 2: 4 x 10000 MGA ->cass 3: 0 x 10000 MGA, cass 4: 0 x 20000 MGA ------->Total cash dispensed: 40000 MGA 21/01/2021 09:53:32: Cash presented 21/01/2021 09:53:32: Waiting for cash to be taken 21/01/2021 09:53:33: Cash taken 21/01/2021 09:53:39: Transaction completed 21/01/2021 09:53:41: Session terminated

暫無
暫無

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

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