0

I am new to regex and python.

My patterns are

'Contact: Order Procesing'
'Contact: [email protected]'
'Contact: Opr & Packaging Supply'
'Contact: JOE (continued)'
'Contact: BOB/LORA/JACKIE'
'Contact: Ben - FTTC CER (continued)'

now I need to find the pattern to match contact and remove the entire string with a blank space.

re.findall(r"Contact:",text)

matches text with Contact. The problem is I do not know how to remove the Contact and right part of the Contact.

Is there any most efficient pythonic way to do this

2
  • print(re.sub(r"^Contact:.*", "", str))? Commented Jun 11, 2019 at 7:12
  • you can also do str.replace("Contact: ","") Commented Jun 11, 2019 at 7:18

2 Answers 2

1

Use re.sub

Ex:

import re

d = ['Contact: Order Procesing', 'Contact: [email protected]', 'Contact: Opr & Packaging Supply', 'Contact: JOE (continued)', 'Contact: BOB/LORA/JACKIE', 'Contact: Ben - FTTC CER (continued)']

for i in d:
    print(re.sub(r"^Contact:.*", "", i))
Sign up to request clarification or add additional context in comments.

Comments

0

If all strings of your list started with the substring 'Contact: ', use this code:

new_list = [string[10:] for string in old_list]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.