Regex to replace email address domains?

后端 未结 2 688
不思量自难忘°
不思量自难忘° 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:49

    SublimeText

    SublimeText uses Boost syntax, which supports quite a large subset of features in Perl regex. But for this task, you don't need all those advanced constructs.

    Below are 2 possible approaches:

    1. If you can assume that @ doesn't appear in any other context (which is quite a fair assumption for normal text), then you can just search for the domain part @[A-Z0-9.-]+\.[A-Z]{2,4}\b and replace it.

    2. If you use capturing groups (pattern) and backreference in replacement string.

      Find what

      \b([A-Z0-9._%-]+)@[A-Z0-9.-]+\.[A-Z]{2,4}\b
      

      ([A-Z0-9._%-]+) is the first (and only) capturing group in the regex.

      Replace with

      $1@fake.com
      

      $1 refers to the text captured by the first capturing group.

    Note that for both methods above, you need to turn off case-sensitivity (indicated as the 2nd button on the lower left corner), unless you specifically want to remove only emails written in ALL CAPS.

提交回复
热议问题