1

I got a .txt file, where the line is a sequence of names in a row:

John Marry Joseph

And I need to extend each of them with @company.com, to get this result:

[email protected] [email protected] [email protected]

Is there a way how to write it with regex or some other method?

1 Answer 1

2

KISS

[(y+"@company.com") for y in x.split()]

Ideone Demo

If you don't want a list then you can join it, like this

print(' '.join([(y+"@company.com") for y in x.split()]))

or this

print('@company.com '.join(x.split()) + '@company.com')

Using regex re.sub

x = "John Marry Joseph"
print(re.sub("([^\s]+)", "\\[email protected]", x))
Sign up to request clarification or add additional context in comments.

4 Comments

This will return an array. Just do " ".join(all that array) to get a string back
@karina well 2nd one can be used then..though good suggestion
Oh sorry. None of that was there when I commented
the second join version works almost ;-) it needs an extra @company.com added to the end.

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.