Capture repeated groups in python regex

后端 未结 2 426
不知归路
不知归路 2021-01-22 03:13

I have a mail log file, which is like this:

Aug 15 00:01:06 **** sm-mta*** to=,

        
2条回答
  •  耶瑟儿~
    2021-01-22 03:57

    Try regex module.

    x="""Aug 15 00:01:06 **** sm-mta*** to=,,user3@aol.com, some_more_stuff
    Aug 16 13:16:09 **** sendmail*** to=, some_more_stuff
    Aug 17 11:14:48 **** sm-mta*** to=,, some_more_stuff"""
    import regex
    print regex.findall(r"sm-mta.*to=\K|\G(?!^).+?@(.*?)[>, ]", x, version=regex.V1)
    

    Output: ['', 'gmail.com', 'yahoo.com', 'aol.com', '', 'gmail.com', 'gmail.com']

    Just ignore the first empty match.

    https://regex101.com/r/7zPc6j/1

提交回复
热议问题