繁体   English   中英

使用Regex匹配python中的字母数字字符列表

[英]Using Regex to match a list of alphanumeric characters in python

我试图逐行解析文件,并查看处理器发出的数据包数据,这些数据基本上用字母数字字符表示。 我在Python中编写了一个正则表达式来读取模式并将数据包存储在列表中。

一个示例行:

Date Time ProcessName ActivityName : 55 34 00 aa c9 00 11 45 55

我的正则表达式:

r'([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s(R.*?:)\s(\d|\D|\s)+$'

我到分组数据[后显示的数字增加: ]成一个列表,并做一些图像处理的活动。 当我运行我的脚本并打印match.group(6)时,它只是在列表中打印了一堆'\\n'

我的剧本片段:

regex = r'([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s([^\s]*?)\s(R.*?:)\s(\d|\D|\s)+$'
pattern = re.compile(regex) 

for line in content:
    match = pattern.search(line)
    if match:
       print match.group(6)

我应该如何使用正则表达式读取一组字母数字字符?

您可以使用re.findall直接取出它。

 (?<=:)\s*([\da-zA-Z]{2}(?:\s[\da-zA-Z]{2})*)

见演示。

https://regex101.com/r/eZ0yP4/13

暂无
暂无

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

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