Regular expressions to structure a non structure texte file

I am new to regular expression and i need to extract some data from the following 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

Here is the code I tried I used group capture but it does not work for some cases and it works for others, also every time I try to combine these expressions I get a “nothing to repeat error”

const CARD_NUMBER = /number:s*([d*]*)/gi;
const TRANSACTION_AMOUNT = /FastCashs*([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[rn]+(.*?)s)",
  "gi"
);
var matches = new Array();
while ((match = rx.exec(sample)) !== null) {
  matches.push(match);
}
console.log(matches);

Answer

So here’s my regex list.

The combined regex is really complex it uses a lot of non-capturing groups and it has to map over the result to remove the undefined matched groups

const str = document.body.innerText;
document.body.innerText = '';
const CARD_NUMBER = /Card number:s*(d{19})/gi;
const TRANSACTION_AMOUNT = /FastCashs*(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})|FastCashs*(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
 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 terminatedCard 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 terminatedCard 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 terminatedCard 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

Leave a Reply

Your email address will not be published. Required fields are marked *