简体   繁体   English

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

[英]Regex: Search for string containing 4 -

I am trying to go through a log file line by line and find instances of strings like 5x76fd63-df62-4dae-a92b-10b8f38fb275 ie having 4 dashes. 我正在尝试逐行浏览日志文件,找到像5x76fd63-df62-4dae-a92b-10b8f38fb275这样的字符串实例,即具有4个破折号。

I need all the lines but want to use matched strings as key/ids. 我需要所有行,但想使用匹配的字符串作为键/ ID。

I need all the lines but want to use matched strings as key/ids. 我需要所有行,但想使用匹配的字符串作为键/ ID。

Since you want keys, a dictionary is appropriate. 由于需要键,因此字典是合适的。

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)

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

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