2

In Notepad++, I am looking to do a regex that replaces this:

Joe Schmoe <joe.schmoe@gmail.com>

with

joe.schmoe@gmail.com

I would've thought that:

^(.*) \<(.*?)\>$

would've put the required value into "\2". However, Notepad++ reports that it finds no matches.

2 Answers2

2

Your problem comes from \< and \> than stand for word boundary. Do not escape these characters if you want to match < and >.


  • Ctrl+H
  • Find what: ^.+?<(.+?)>
  • Replace with: $1
  • TICK Wrap around
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

^               # beginning of line
.+?             # 1 or more any character but newline, not greedy
<               # literally
(.+?)           # group 1, 1 or more any character but newline, not greedy
>               # literally

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Be aware that will not work for email like "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com

Toto
  • 89,455
  • 62
  • 89
  • 125
1

You can use the following regex to match your whole line of text:

.*<([.\w]+@\w+\.\w+)>

Then you can replace with \1, containing your email only. Make sure to have "Regular Expression" option checked.

Regex Explanation:

  • .*: any sequence of characters
  • <: open angular parenthesis
  • ([.\w]+@\w+\.\w+): Group containing the email
    • [\.\w]+: email name
    • @: @
    • \w+(?:\.\w+): the domain
  • >: closed angular parenthesis

Check the regex demo here.

Note: If you need a more complex email pattern matching, you can look this thread.

lemon
  • 14,875
  • 6
  • 18
  • 38
  • `[.\w]+` is not enough for an email. https://en.wikipedia.org/wiki/Email_address#Examples – Toto May 21 '23 at 08:56
  • This is the reason why there's a note at the bottom of my answer. – lemon May 21 '23 at 09:19
  • 1
    Hi there, in your pattern you don't have to escape the dot in the character class and you can omit the non capture group in the last part `.*<([.\w]+@\w+\.\w+)>` (or repeat it 1 or more times as the group by itself this way has no purpose) – The fourth bird May 21 '23 at 09:28
  • 1
    Thanks for sharing, didn't know about the dot escape inside the character class. – lemon May 21 '23 at 09:33