正则表达式:搜索包含4的字符串-

时间:2019-03-25 22:38:18

标签: python regex python-3.x

我正在尝试逐行浏览日志文件,并找到类似5x76fd63-df62-4dae-a92b-10b8f38fb275这样的字符串实例,即具有4个破折号。

我需要所有行,但想使用匹配的字符串作为键/ ID。

1 个答案:

答案 0 :(得分:1)

  

我需要所有行,但想使用匹配的字符串作为键/ ID。

由于需要键,因此字典是合适的。

import re
keyline = {}                                    # start with empty dictionary
for line in file:
    m = re.search("\w+-\w+-\w+-\w+-\w+", line)  # search ID with four dashes
    if m:
        keyline[m.group()] = line               # store the line with its ID

print(keyline)
相关问题