检测电子邮件使用正则表达式的文本 [英] Detect email in text using regex
问题描述
我要检测文本格式的邮件,这样我就可以把对他们的锚标记与标签上一篇锚。我有正则表达式的,但代码还可以检测它已经由锚标记封装或锚标记的mailto参数的内部电子邮件
I want to detect emails in text format so that I can put an anchor tag over them with mailto tag in anchor. I have the regex for it but the code also detects emails which are already encapsulated by anchor tag or is inside the anchor tag mailto parameter.
我的正则表达式是:
([\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?)
但它检测到3在下面的示例文本匹配:
But it detects 3 matches in the following sample text:
ttt <a href='mailto:someone@example.com'>someemail@mail.com</a> abc email@email.com
我只想email@email.com通过正则表达式匹配。
I want only email@email.com to be matched by the regex.
推荐答案
非常相似,我的以前回答您的其他问题,试试这个
Very similar to my previous answer to your other question, try this
(?<!(?:href=['"]mailto:|<a[^>]*>))(\b[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?)
的唯一的事情是真的不同的是,电子邮件开始之前,边界 \b
的单词。
The only thing that is really different is the word boundary \b
before the start of the email.
请参阅类似表述的在这里Regexr ,它不完全一样,因为Regexr不支持在后向交替和无限长度。
See a similar expression here on Regexr, its not exactly the same, because Regexr does not support alternations and infinite length in the lookbehind.
这篇关于检测电子邮件使用正则表达式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!