英文:
Regex with escaped less-than and greater-than around e-mail address?
问题
在Notepad++中,我要做一个正则表达式替换,将这部分内容:
Joe Schmoe <joe.schmoe@gmail.com>
替换为:
joe.schmoe@gmail.com
我原本以为这个正则表达式:
^(.*) \<(.*?)\>$
会将所需的值放入"\2
"中。然而,Notepad++报告说它找不到匹配项。
英文:
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.
答案1
得分: 2
Your problem comes from \<
and \>
than stand for word boundary. Do not escape these characters if you want to match <
and >
.
- <kbd>Ctrl</kbd>+<kbd>H</kbd>
- Find what:
^.+?<(.+?)>
- Replace with:
$1
- TICK Wrap around
- SELECT Regular expression
- UNTICK
. matches newline
- <kbd>Replace all</kbd>
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):
Screenshot (after):
Be aware that will not work for email like "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
英文:
Your problem comes from \<
and \>
than stand for word boundary. Do not escape these characters if you want to match <
and >
.
- <kbd>Ctrl</kbd>+<kbd>H</kbd>
- Find what:
^.+?<(.+?)>
- Replace with:
$1
- TICK Wrap around
- SELECT Regular expression
- UNTICK
. matches newline
- <kbd>Replace all</kbd>
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):
Screenshot (after):
Be aware that will not work for email like "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
答案2
得分: 1
以下是已翻译的内容:
您可以使用以下正则表达式来匹配您整行的文本:
.*<([.\w]+@\w+\.\w+)>
然后,您可以用\1
来替换,其中包含您的电子邮件地址。请确保选中“正则表达式”选项。
正则表达式解释:
.*
:任意字符序列<
:打开角括号([.\w]+@\w+\.\w+)
:包含电子邮件的组[\.\w]+
:电子邮件名称@
:@\w+(?:\.\w+)
:域名
>
:关闭角括号
在此处查看正则表达式演示 here。
注意:如果您需要更复杂的电子邮件模式匹配,可以查看 this thread。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论