Home > database >  Regex match the first line after blank line
Regex match the first line after blank line

Time:12-26

I want to check the first line after blank line I tried with ^$.* but not working,

YYYY-MM-DD HH:II:SS Message Log
Message Log

Message Log
not empty line

the desired output : ["YYYY-MM-DD HH:II:SS Message Log","Message Log"]

CodePudding user response:

Not sure why are you using a Regex for this task, I think you can use a simple split.

data = '''YYYY-MM-DD HH:II:SS Message Log
Message Log

Message Log
not empty line'''

relevant_part, _ = data.split('\n\n', 1)
relevant_part.splitlines() # ["YYYY-MM-DD HH:II:SS Message Log","Message Log"]

CodePudding user response:

You could use:

import re


pattern = re.compile("(?:^|\n\n)(.*)")
pattern.findall(data)

OUTPUT

['YYYY-MM-DD HH:II:SS Message Log', 'Message Log']

To test it, I have saved these lines in a file:

YYYY-MM-DD HH:II:SS Message Log
Message Log

Message Log
not empty line

read the content in data and applied the pattern to it.

  • Related