Regex to replace email address domains?

后端 未结 2 618
不思量自难忘°
不思量自难忘° 2021-01-16 01:49

I need a regex to obfuscate emails in a database dump file I have. I\'d like to replace all domains with a set domain like @fake.com so I don\'t risk sending ou

2条回答
  •  遥遥无期
    2021-01-16 02:36

    You may use the following command for Vim:

    :%s/\(\<[A-Za-z0-9._%-]\+@\)[A-Za-z0-9.-]\+\.[A-Za-z]\{2,4}\>/\1fake.com/g
    

    Everything between \( and \) will become a group that will be replaced by an escaped number of the group (\1 in this case). I've also modified the regexp to match the small letters and to have Vim-compatible syntax.

    Also you may turn off the case sensitivity by putting \c anywhere in your regexp like this:

    :%s/\c\(\<[A-Z0-9._%-]\+@\)[A-Z0-9.-]\+\.[A-Z]\{2,4}\>/\1fake.com/g
    

    Please also note that % in the beginning of the line asks Vim to do the replacement in a whole file and g at the end to do multiple replacements in the same line.

    One more approach is using the zero-width matching (\@<=):

    :%s/\c\(\<[A-Z0-9._%-]\+@\)\@<=[A-Z0-9.-]\+\.[A-Z]\{2,4}\>/fake.com/g
    

提交回复
热议问题