简体   繁体   English

使用正则表达式从文本文件中提取数据

[英]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: output是这样的:

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:在第四列“4000 MGA”中,我想将其分成两列,分别是“4000”和“MGA”,我尝试使用以下表达式,但没有得到任何结果:

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:您可以将一个捕获组分成 2 个并用空格将它们分开:

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正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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