简体   繁体   中英

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:"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM