danishxr
danishxr

Reputation: 89

How to remove right side elements of a string with regex in python?

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

Upvotes: 0

Views: 350

Answers (2)

alex romanoff
alex romanoff

Reputation: 21

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

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

Upvotes: 0

Rakesh
Rakesh

Reputation: 82785

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))

Upvotes: 1

Related Questions