Search code examples
regexemailnotepad++

Regex with escaped less-than and greater-than around e-mail address?


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.


Solution

  • 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