Home > Net >  Python - using Regex findall strings after some fixed text
Python - using Regex findall strings after some fixed text

Time:02-02

I'm new to Python and am getting confused. I have a list of email strings and want to get the string after some specific text and before some text. So from the list below;

Email: [email protected]
Email: [email protected]
Email: [email protected]

get the following;

promo@madeup
dave@madeup
john@madeup

I've managed to get the first string (below) but can't seem to get all of them

import re

lines = '''
Email: [email protected]
Email: [email protected]
Email: [email protected]
'''


emailAddr = lines.split('Email:')[1] #start
emailAddr2 = emailAddr.split('.com')[0] #end
print(emailAddr2)

CodePudding user response:

Use a lookbehind to match the Email: prefix before each email and the .com suffix.

emails = re.findall(r'(?<=Email: ).*(?=\.com)', lines)
print(emails)

Or just loop through the lines and remove the Email: prefix.

emails = [s.replace('Email: ', '').replace('.com') for s in lines.splitlines() if s]
print(emails)

CodePudding user response:

lines = '''
Email: [email protected]
Email: [email protected]
Email: [email protected]
'''

lines = lines.split('\n')
output = [x.split('Email:')[1].replace('.com','').strip() for x in lines if 'Email:' in x]
print(output)

will output:

['promo@madeup', 'dave@madeup', 'john@madeup']

if you want them all printed on a new line:

print('\n'.join(output))

will output:

promo@madeup
dave@madeup
john@madeup
  • Related