1

I have strings as below:

s1 = "My email Id is abcd@g mail.com"
s2 = "john@ hey.com is my email id"
s3 = "id is rock@gmail .com"
s4 = "The id is sam @yahoo.in"

I have to replace the blank space in email ID using regex. How can I achieve this?

I tried

s = re.sub(r'@\w*[\s]+[\w]*\.', r'', s1)

which is giving me output as:

'My email Id is abccom'

Output should be:

'My email Id is [email protected]' 

I'm not sure how can I replace only blank value with re.sub.

Any Suggestions are welcome

Thanks,

0

2 Answers 2

2

You can use a callable to remove spaces after matching email addresses with spaces using re.sub.

import re
l = [
    "My email Id is abcd@g mail.com",
    "john@ hey.com is my email id",
    "id is rock@gmail .com",
    "The id is sam @yahoo.in"
]
for s in l:
    print(re.sub(r'[\w.-]+ ?@(?:[\w-]+\.[\w -]+|[\w -]+\.[\w-]+)', lambda e: e[0].replace(' ', ''), s))

This outputs:

My email Id is [email protected]
[email protected] is my email id
id is [email protected]
The id is [email protected]
Sign up to request clarification or add additional context in comments.

2 Comments

I made small change in the question. It works fine if email id is at the end but it's removing spaces between other words to if email id is not at the end. See 2nd string the question.
I see. Edited my answer accordingly then.
1

You can use back references in re.sub (online regex here):

import re

data = [
"My email Id is abcd@g mail.com",
"Email Id: defg@yah oo.com",
"id is rock@gmail .com"
]

for s in data:
    print(re.sub(r'(@.*)(\s+)(.*)', r'\1\3', s))

Prints:

My email Id is [email protected]
Email Id: [email protected]
id is [email protected]

EDIT:

If the blank space is before the @, the regexp is a little bit tricky (to not match e.g. "aaa bbb ccc [email protected]", online regex here):

import re

data = [
"My email Id is ab [email protected]",
"Email Id: def [email protected]",
"id is roc [email protected]",
"aaa bbb ccc [email protected]"
]

for s in data:
    print(re.sub(r'(?=is|:)(.*)\s+(.*@.*)', r'\1\2', s))

Prints:

My email Id is [email protected]
Email Id: [email protected]
id is [email protected]
aaa bbb ccc [email protected]

Now we can combine these regexes:

import re

data = [
"My email Id is ab [email protected]",
"Email Id: def g@ya hoo.com",
"id is roc k@gm ail.com",
"aaa bbb ccc [email protected]"
]

for s in data:
    s = re.sub(r'(@.*)\s+(.*)', r'\1\2', s)
    s = re.sub(r'(?=is|:)(.*)\s+(.*@.*)', r'\1\2', s)
    print(s)

Will print:

My email Id is [email protected]
Email Id: [email protected]
id is [email protected]
aaa bbb ccc [email protected]

3 Comments

I have edited my question with one more condition if the blank space is before @
it's not handling if email id is not at the end. See 2nd string in my question.
@AkshayNevrekar Just combine these regexes, see my updated answer

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.