user9510048
user9510048

Reputation:

Extracting data from text file using regular expressions

I have a text file that I am trying to extract data from using regular expressions, here is a sample of the text file :

 Card number: 9999*********2789, SEQ: 195
Current session ID: 175
 21/01/2021 09:53:41 : Session terminated

Here is the regular expression I am using to get most of the data i want :

regex = r"number:\s(\d+\*+\d+).*?ID:\s*(\d*).*?ATM:\s(\w+).*?STAN:\s(\d+).*?Total cash dispensed:\s*([a-zA-Z0-9 ]*).*?completed[\r\n]+(.*?)\s:"

The output is like this:

In the fourth column '4000 MGA' i want to have it separated into two columns with '4000' and 'MGA', I tried using the following expression but I get no results :

regex = r"number:\s(\d+\*+\d+).*?ID:\s*(\d*).*?ATM:\s(\w+).*?STAN:\s(\d+).*?cass 1:\s*\d*([a-zA-Z ]*).?Total cash dispensed:\s*([0-9 ]*).*?completed[\r\n]+(.*?)\s:"

Upvotes: 1

Views: 480

Answers (1)

anubhava
anubhava

Reputation: 784918

You can just break that one capture group into 2 and separate them out with whitespaces:

regex = r"number:\s(\d+\*+\d+).*?ID:\s*(\d*).*?ATM:\s(\w+).*?STAN:\s(\d+).*?Total cash dispensed:\s*([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+).*?completed[\r\n]+(.*?)\s:"

RegEx Demo

Upvotes: 1

Related Questions