soulowit
soulowit

Reputation: 11

Regular Expression Delete Everything Before Line

I am trying to use a regex to delete everything before a certain line in a multi-line string. Is there a regex expression that captures everything before (and including) an expression?

import re

sample = '''
This is content I need to delete
I do not need any of this.

===

Text I need 
Is here'''

content = re.sub(r'\n===', "", sample)

print(content)

Upvotes: 0

Views: 1176

Answers (2)

Keiwan
Keiwan

Reputation: 8251

If you want to be left with just

Text I need
Is here

(so without any more newlines after the ===) you can use

content = re.sub(r'(.|\n)*===\n*', "", sample)

The (.|\n)* will get rid of all the text and newlines up to the === and the \n* will delete the following newlines. You can also leave this last part out if you want to keep the newlines after ===. So

content = re.sub(r'(.|\n)*===', "", sample)

will result in

             // newline 
             // newline
Text I need 
Is here

There will be two newlines left (one directly after the === and the second one for the empty line). If you just want one newline before Text I need... then use:

r'(.|\n)*===\n'

Upvotes: 0

Ravi Kumar
Ravi Kumar

Reputation: 445

You are not grabbing characters which occur before \n===. You can use this.

content = re.sub(r'.*\n===', "", sample, flags=re.DOTALL)

Upvotes: 1

Related Questions