替换字符串(名称+姓氏到电子邮件)正则表达式

时间:2018-09-26 14:21:00

标签: regex replace

我有一个很大的.doc文件,其中包含很多客户信息,我需要使用以下逻辑查找/替换:

Surname1 Surname2, Name -> Name.Surname1.Surname2@xxxxxx.com

我试图弄乱notepad ++,但我实际上并不具备正则表达式所需的知识。

来自评论

Jordan Parks
Kevin Johanson Larsson
Oleg Garcia Cruz
Jose Rondo Curry
Tim Bush Gallagher
Victoria 

1 个答案:

答案 0 :(得分:1)

使用记事本++,您可以执行以下操作:

  • Ctrl + H
  • 查找内容:^(\S+)\h*(\S+)?\h*(\S+)?$
  • 替换为:(?3$3.$1.$2:(?2$2.$1:$1))@xx.com
  • 检查环绕
  • 检查正则表达式
  • 全部替换

说明:

^           : beginning of line
  (\S+)     : group 1, 1 or more non space character
  \h*       : 0 or more horizontal spaces
  (\S+)?    : optional group 2, 1 or more non space character
  \h*       : 0 or more horizontal spaces
  (\S+)?    : optional group 3, 1 or more non space character
$           : end of line

替换:

(?3         : if group 3 exists
  $3.$1.$2  : concatenate group 3 (name) then group 1 (surname1) then group 2 (surname2)
  :         : else (group 3 does not exist)
  (?2       : if group 2 exists
    $2.$1   : concatenate group 2 (name) then group 1 (surname1)
    :       : else, group 2 does not exist
    $1      : group 1 (name)
  )
)
@xx.com     : literally 

给出:

Jordan Parks
Kevin Johanson Larsson
Oleg Garcia Cruz
Jose Rondo Curry
Tim Bush Gallagher
Victoria 

给定示例的结果

Parks.Jordan@xx.com
Larsson.Kevin.Johanson@xx.com
Cruz.Oleg.Garcia@xx.com
Curry.Jose.Rondo@xx.com
Gallagher.Tim.Bush@xx.com
Victoria@xx.com