简体   繁体   English

正则表达式以提取特定位置的数字

[英]Regular Expression to extract numbers in specific location

I'm programming in python and I need a regular expression that will extract the number between | 我正在用python编程,我需要一个正则表达式来提取|之间的数字。 | | in these dataset and save the result for reuse. 在这些数据集中保存结果以供重用。 So it should extract 66 from 1st line, 1032 from the 2nd, 1472 from 3rd and so on. 因此,它应该从第一行提取66,从第二行提取1032,从第三行提取1472,依此类推。 I'm new to programming and regular expression. 我是编程和正则表达式的新手。 Any help will be much appreciated! 任何帮助都感激不尽!

232404811.111146|66|ip:tcp
232404811.111556|1032|ip:udp:data
232404811.112015|1472|ip:tcp:http:data
232404811.112060|1472|ip:tcp:http:data

The data is in a file & I will loop through it line by line. 数据在文件中,我将逐行循环通过它。

You don't need a regex: 您不需要正则表达式:

numbers_list = []
with open(filename) as f:
    for line in f:
        numbers_list.append(line.split("|")[1]

This will split each line on the | 这将拆分|上的每一行。 character, and take the second field (remember indexing is 0-based). 字符,然后选择第二个字段(请记住索引从0开始)。

If you're feeling fancy, do the whole thing in a list comprehension (thanks Padraic ): 如果您觉得自己很花哨,请对列表进行完整的理解(感谢Padraic ):

with open(filename) as f:
    numbers_list = [line.split("|")[1] for line in f]

To match this with regex use: 要将其与正则表达式匹配,请使用:

^\d+\.\d+\|(\d+)\|.*?$

The match is in capturing group 1 比赛在第1组中

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

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