【问题标题】:Using Regex to match a list of alphanumeric characters in python使用正则表达式匹配python中的字母数字字符列表
【发布时间】:2014-12-11 18:01:27
【问题描述】:

我正在尝试逐行解析文件并查看处理器发出的数据包数据,这些数据基本上以字母数字字符表示。我用 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'

我的脚本的 sn-p:

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)

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

【问题讨论】:

    标签: python regex alphanumeric


    【解决方案1】:

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

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

    查看演示。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2016-09-01
      • 1970-01-01
      • 2022-08-17
      • 2013-06-03
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多