0

Looking for a good way to remove the "@domain" of email addresses after an initial user prompt.

prompt = input("Enter the email address of the user: ")

All of the domains will be the same so I don't need to worry about sub domains or any other weirdness.

Input: john.doe@generic.com Output: john.doe

I'd like the output to go into another variable for use in a series of bash commands on a Linux server.

tychill
  • 13
  • 1
  • 2
  • All that has been posted is a program description. However, we need you to ask a question according to the [ask] page. We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know what is on-topic here by visiting the [help/on-topic]; asking us to write the program for you, suggestions, and external links are off-topic. – Patrick Artner Mar 22 '19 at 23:51
  • Seriously - you can use `split("@")` - you can iterate charewise until you find `"@"` - you can use `str.find("@")` to get the index and use string slicing, you can use `re`gex, you can use `str.partition` ... yet you found _no_ way to solve it? – Patrick Artner Mar 22 '19 at 23:53
  • Hey Alain, thank you! I'm completely new to Python and scripting in general. Not sure what I was doing wrong with all of the other suggestions in other threads, but I was getting errors. Your suggestion just simply worked :) Problem solved. – tychill Mar 25 '19 at 15:26

2 Answers2

6

you could simply use:

prompt.split("@")[0]
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

How about regular expressions?

import re

def extract_user(address):
    result = re.search(r'([\w\d\.]+)@[\w\d\.]+', address)

    if result and address.count('@') == 1:
        return result.group(1)

    else:
        raise ValueError(f'{address} is not a validly formatted e-mail address.')

extract_user('john.doe@generic.com')

Output:

'john.doe'
gmds
  • 19,325
  • 4
  • 32
  • 58