简体   繁体   English

提取数字和它后面的字符串

[英]extract a number and the string after it

i have a script that parse log files and in a part of programming i have to know if a message is transmitted or not and by reading those lines i can get the Message id and know wether the message is trasmitted or not. 我有一个解析日志文件的脚本,并且在编程的一部分中,我必须知道消息是否已传输,并且通过阅读这些行,我可以获得消息ID,并且知道消息是否被传输。

01:09:25.258 mta   Messages       I Doc O:NVS:SMTP/alarm@yyy.xx R:NVS:SMS/+654811 mid:6261
01:09:41.965 mta   Messages       I Rep 6261 OK, Message received(ID: 26)
08:14:14.469 mta   Messages       I Doc O:NVS:SMTP/alarm@xxxx.en R:NVS:SMS/+654646 mid:6262
08:14:30.630 mta   Messages       I Rep O:NVS:SMTP/alarm@azea.er R:NVS:SMS/+304859 mid:6262
08:14:30.630 mta   Messages       I Rep 6262 Error while transmitting (ID: 28)

The lines i'm interested in are the second and the last, i'd like to extract the 6261 and the ok after it and same for the last line 我感兴趣的行是第二行和最后一行,我想提取6261和它后面的行,最后一行相同

You don't need regexp. 您不需要正则表达式。 Just split the lines on the whitespace. 只需在空白处分割线即可。

>>> line.split(None, 5)
['10:56:45.255', 'Message', 'I', 'Rep', '2559', 'OK, Message received']

Since you only want the ID and message: 由于您只需要ID和消息:

>>> [line.split(None, 5)[-2:] for line in file.readlines()]
[['2548', 'OK'], ['2559', 'OK, Message received'], ['2560', 'Error'], ['2561', 'Transmission... ']]

Note that the spaces in the message is NOT a problem. 请注意,消息中的空格不是问题。

/[0-9]{4} (.*)/ would fit the purpose, but I don't know if that's generic enough for you. /[0-9]{4} (.*)/可以满足目的,但是我不知道这对您来说是否足够通用。 Depending on whether the line id (2548 etc.) can also be shorter the regexp would have to be adapted slightly, but from the 4 shown lines this would work. 根据行ID(2548等)是否也可以更短,必须稍微修改正则表达式,但是从显示的4行中可以这样做。

When writing regular expressions the most important thing is is not to work from 'samples' alone, but also from 'usable assumptions' about the strings you are trying to match. 在编写正则表达式时,最重要的不是仅凭“样本”工作,还不是根据您要匹配的字符串的“可用假设”进行工作。 I cannot reliably say this solution perfectly solves your problem because I don't know the entire problem, and as such cannot supply a perfect pattern. 我无法可靠地说此解决方案可以完美解决您的问题,因为我不了解整个问题,因此无法提供理想的模式。

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

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