How do I change two letters closest to a string and one letter immediately after a string using Notepad++?

8

2

I have a list of emails, and I want to change the two letters before "@" and the first letter after "@" using Notepad++.

For example:

username@yourdomain.com

becomes

userna**@*ourdomain.com

loveman2019

Posted 2019-03-15T08:02:12.357

Reputation: 109

9Just an obvious remark, the concrete example you gave shows how useless this pattern would be to anonymize email addresses. It’s usually better like x****@y*.com – eckes – 2019-03-15T11:11:32.823

@eckes would that even be possible in N++? – WELZ – 2019-03-15T17:13:06.270

3@WELZ Yes but its more work, a half working sample would (.)[^@]*@([^.]).*(\.[a-z]+) use 3 capture groups which you can address in the replace with string: \1***@\2***\3 - uses a fixed number of mask characters but this is actually good. – eckes – 2019-03-15T18:17:34.900

Answers

31

I want to change the two letters before "@" and the first letter after "@"

  • Menu "Search" > "Replace" (or Ctrl + H)

  • Set "Find what" to ..@.

  • Set "Replace with" to **@*

  • Enable "Regular expression"

  • Click "Replace All"

    enter image description here

Before:

username@yourdomain.com

After:

userna**@*ourdomain.com

Further reading

DavidPostill

Posted 2019-03-15T08:02:12.357

Reputation: 118 938

DavidPostill thanks,it worked for me. – loveman2019 – 2019-03-15T08:17:05.260

I'd say it should be .?.@. as there might not be two characters before @. – n0rd – 2019-03-16T18:55:11.820

1@n0rd The question specified two characters, but you are correct if there is only one. – DavidPostill – 2019-03-16T19:07:39.190

10

You can do this by using a regex search/replace.

At the bottom, select Regular Expression.

In the Search for entry, you type in: ..@. In the Replace with, you type in **@*

Then press the button Replace All

This works because Regex searches will only replace if its search criteria matches exactly. The match is explained as follows:

..@. There are 3 dots and an @:

  • @ has no special meaning in regex so it means a literal @.
  • . means any character, exactly once. By writing .. it means 2 characters of any kind, as long as there are 2 characters.

LPChip

Posted 2019-03-15T08:02:12.357

Reputation: 42 190