我的想法是找到句子中的每封电子邮件,并将其替换为不同的随机电子邮件(匿名化(。但我无法得到我想要的结果。每封电子邮件都被替换为相同的电子邮件,或者我收到错误(列表索引超出范围(
输入: 电子邮件 ="daniel@hotmail.com 已将其发送给 ana@gmail.com">
我想要的输出 电子邮件 ="albert@hotmail.com 已将其发送给 john@gmail.com">
random_emails = ["albert", "john", "mary"]
def find_email(email: str):
result = email
i = 0
email_address = r"S+@"
for text in email:
result = re.sub(email_address, random_emails[i] + "@", result)
i += 1
return result
print(find_email(email))
我找到了一个解决方案,但请注意,相同的电子邮件将以相同的方式匿名化。我让你试试这个:
import re
email = "daniel@hotmail.com sent it to ana@gmail.com"
random_emails = ["albert", "john", "mary"]
def find_email(email: str):
result = email
i = 0
email_address = r"S+@"
regex_matches = re.findall(email_address, email)
for match in regex_matches:
result = result.replace(match, random_emails[i] + "@")
i += 1
return result
print(find_email(email))
你不需要循环,我认为你的正则表达式可以改进
def find_email(email):
result = email
email_address = r"(w+@)(w+.* )(w+@)(w+.*)"
a='AAAAA@'
b='BBBBB@'
result = re.sub(email_address, rf'{a}2{b}4', result)
return result
email = "daniel@hotmail.com sent it to ana@gmail.com"
print(find_email(email))
解释:
您可以创建替换组:
1º = 1º 电子邮件 2º =服务器和文本 3º = 2º 电子邮件 4º = server.com
现在,您只需要将\1和\2替换为您想要的每个想法
示例 2:您的新例程
import re
from random import seed
from random import randint
random_emails = ["albert", "john", "mary"]
def find_email(email):
result = email
email_address = r"(w+@)(w+.* )(w+@)(w+.*)"
first = randint(0, 2)
second = randint(0, 2)
while first == second:
second = randint(0, 2)
result = re.sub(email_address, rf'{random_emails[first]}@2{random_emails[second]}@4', result)
return result
email = "daniel@hotmail.com sent it to ana@gmail.com"
print(find_email(email))
我使用随机生成一个随机数以从列表中获取电子邮件。 而"虽然第一个==第二个:"只是为了不重复第一个和第二个 电子邮件