Search code examples
pythonregexstringextend

Python regex - extend string


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?


Solution

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