Я безуспешно пытался расширить это решение с помощью regex101. Компиляция объекта re (шаблона), как предложено в этом решении, кажется слишком сложной для этой задачи. Я просмотрел сообщения о проверке адресов электронной почты, но они описывают другую проблему. Ниже моего кода:
Код: Выделить всё
import re
#creating some data
test = ['some random text maya @ proton.me with some more text maya+lucco@proton.me',
'maya-lucco@proton.me with another address maya_lucco@proton.me',
'some text maya.lucco @proton.me with some more bla maya.lucco@proton.me',
'maya-lucco_@proton.me more text maya@ proton.me '
]
test = pd.DataFrame(test, columns = ['words'])
#creating a function because I like to add some other data cleaning to it later on
def anonymiseEmail(text):
text = str(text) #make text as string variable
text = text.strip() #remove any leading, and trailing whitespaces
text = re.sub(r'\S*@\S*\s?', '{e-mail}', text) #remove e-mail address
return text
# applying the function
test['noEmail'] = test.words.apply(anonymiseEmail)
#checking the results
print(test.noEmail[0])
Код: Выделить всё
Output: some random text maya {e-mail}proton.me with some more text {e-mail}
Как можно расширить код, чтобы весь адрес электронной почты, независимо от количества в нем пробелов, заменялся размещенным держателем или удалялся ?
Обновление после комментариев:
Я изучил просмотр вперед и назад в RegEx, т.е. (?= @) и (?
Подробнее здесь: https://stackoverflow.com/questions/785 ... hitespaces