正则表达式中转义的小于号和大于号,围绕电子邮件地址?

huangapple go评论54阅读模式
英文:

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: ^.+?&lt;(.+?)&gt;
  • 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
&lt;               # literally
(.+?)           # group 1, 1 or more any character but newline, not greedy
&gt;               # literally

Screenshot (before):

正则表达式中转义的小于号和大于号,围绕电子邮件地址?

Screenshot (after):

正则表达式中转义的小于号和大于号,围绕电子邮件地址?

Be aware that will not work for email like &quot;very.(),:;&lt;&gt;[]\&quot;.VERY.\&quot;very@\\ \&quot;very\&quot;.unusual&quot;@strange.example.com

英文:

Your problem comes from \&lt; and \&gt; than stand for word boundary. Do not escape these characters if you want to match &lt; and &gt;.


  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: ^.+?&lt;(.+?)&gt;
  • 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
&lt;               # literally
(.+?)           # group 1, 1 or more any character but newline, not greedy
&gt;               # literally

Screenshot (before):

正则表达式中转义的小于号和大于号,围绕电子邮件地址?

Screenshot (after):

正则表达式中转义的小于号和大于号,围绕电子邮件地址?

Be aware that will not work for email like &quot;very.(),:;&lt;&gt;[]\&quot;.VERY.\&quot;very@\\ \&quot;very\&quot;.unusual&quot;@strange.example.com

答案2

得分: 1

以下是已翻译的内容:

您可以使用以下正则表达式来匹配您整行的文本:

.*&lt;([.\w]+@\w+\.\w+)&gt;

然后,您可以用\1来替换,其中包含您的电子邮件地址。请确保选中“正则表达式”选项。

正则表达式解释

  • .*:任意字符序列
  • &lt;:打开角括号
  • ([.\w]+@\w+\.\w+):包含电子邮件的组
    • [\.\w]+:电子邮件名称
    • @:@
    • \w+(?:\.\w+):域名
  • &gt;:关闭角括号

在此处查看正则表达式演示 here

注意:如果您需要更复杂的电子邮件模式匹配,可以查看 this thread

英文:

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

.*&lt;([.\w]+@\w+\.\w+)&gt;

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

Regex Explanation:

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

Check the regex demo here.

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

huangapple
  • 本文由 发表于 2023年5月21日 05:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定