Py学习  »  Python

从python中的地址中删除电子邮件域[重复]

tychill • 5 年前 • 1590 次点击  

在初始用户提示后寻找删除电子邮件地址“@domain”的好方法。

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

所有的域都是相同的,所以我不需要担心子域或任何其他的奇怪。

输入:john.doe@generic.com 输出:john.doe

我希望输出进入另一个变量,以便在Linux服务器上的一系列bash命令中使用。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38992
 
1590 次点击  
文章 [ 2 ]  |  最新文章 5 年前
gmds
Reply   •   1 楼
gmds    6 年前

正则表达式怎么样?

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

输出:

'john.doe'
Alain T.
Reply   •   2 楼
Alain T.    6 年前

您可以简单地使用:

prompt.split("@")[0]