正则表达式替换电子邮件地址域? [英] Regex to replace email address domains?
问题描述
我目前有这个正则表达式查找电子邮件
\b [A-Z0-9 ._% - ] + @ [A-Z0-9 .-] + \ 。[AZ] {2,4} \b
如何将此搜索正则表达式转换为正则表达式我可以在Sublime Text或SED或Vim中查找和替换操作?
编辑:
只是一个注释,我只是意识到我可以替换 @ [A-Z0-9 .-] + \。[AZ] {2,4}在这种情况下,\b
,但在学术上我仍然对如何将电子邮件正则表达式的每个部分作为令牌进行处理感兴趣,并独立地替换用户名/域。
SublimeText
崇高文字使用 Boost语法,它支持Perl正则表达式中相当大的功能子集。但是对于这个任务,你不需要所有的高级结构。
以下是两种可能的方法:
-
如果您可以假设
@
不会出现在任何其他上下文中(这对于正常文本是相当公平的假设)那么你可以搜索域名部分@ [A-Z0-9 .-] + \。[AZ] {2,4} \b
并替换它 -
如果您使用捕获组
(pattern)
和替换字符串中的反向引用。
查找
\b([A -Z0-9 ._% - ] +)@ [A-Z0-9 .-] + \。[AZ] {2,4} \b
([A-Z0-9 ._% - ] +)
是第一个(而且只有)捕获
替换为
$1@fake.com
$ 1
是指由第一个捕获组捕获的文本。
请注意,对于我您需要关闭区分大小写(表示为左下角的第二个按钮),除非您特别要仅删除所有CAPS中写入的电子邮件。
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 out emails to real people during development. The emails do have to be unique to match database constraints, so I only want to replace the domain and keep the usernames.
I current have this regex for finding emails
\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b
How do I convert this search regex into a regex I can use in a find and replace operation in either Sublime Text or SED or Vim?
EDIT:
Just a note, I just realized I could replace all strings found by @[A-Z0-9.-]+\.[A-Z]{2,4}\b
in this case, but academically I am still interested in how you could treat each section of the email regex as a token and replace the username / domain independently.
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:
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.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.
这篇关于正则表达式替换电子邮件地址域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!