Regular Expressions 101

Community Patterns

Community Library Entry

4

Regular Expression
PCRE (PHP <7.3)

/
(?<=.).(?=[^@]*?.@)|(?:(?<=@.)|(?!^)\G(?=[^@]*$))(.)(?=.*\.)|(?:(?<=\.)(?=[^.]+$)|(?!^)\G(?=[^@.]*$))[^.](?!$)
/
gm

Description

Replaces second to last but one characters in email with asterisks (@ and last . are protected): john.travolta@gmail.com > j***********a@g****.**m

Submitted by Wiktor Stribiżew - 9 years ago