[英]Match Everything Except Email With This Regex Pattern
So, I have rows of text with plenty unnecessary information in them (in a google sheet). 因此,我有一行文本,其中包含大量不必要的信息(在Google工作表中)。 I would like to match everything except the EMAIL for which I'm using the following regex:
我想匹配除我正在使用以下正则表达式的EMAIL以外的所有内容:
[a-zA-Z0-9_.+-]+@(?:[a-zA-Z0-9-]+\.)+(?!png|jpg|gif)[a-zA-Z0-9-]+
If I can manage to match everything except the email, then I can just find/replace and leave only the email in the row which is what I want. 如果我可以匹配电子邮件以外的所有内容,那么我可以查找/替换并仅将电子邮件保留在我想要的行中。 Having some trouble here.
在这里遇到麻烦。 Help would be appreciated!
帮助将不胜感激!
While it's not perfect this could be what you're after: 虽然这并不完美,但这可能是您追求的目标:
For the online demo this works: ^(?:.*?(\\w[^@\\s]*@[^@\\s]{2,}).*?|.+)$
demo 对于在线演示,此方法有效:
^(?:.*?(\\w[^@\\s]*@[^@\\s]{2,}).*?|.+)$
演示
However for Google Sheets you need to remove the ^
and $
line start/end markers and it should do most of what you want. 但是,对于Google表格,您需要删除
^
和$
行的开始/结束标记,并且该标记应该可以完成大部分操作。 So: 所以:
(?:.*?(\\w[^@\\s]*@[^@\\s]{2,}).*?|.+)
replace this pattern with $1 to leave just the email address per line 用$ 1替换此模式以仅保留每行的电子邮件地址
This works per line, the pattern is made up of two patterns in a non-capturing group (?:
. First pattern looks from the start of the line .*?
to lazily match all characters up until group1 containing the email pattern (\\w[^@\\s]*@[^@\\s]{2,})
followed by anything else .*
till the end of the line. The second pattern will match all other lines without an email. This is the search pattern. The replace pattern is simply group1 $1
. Group1 will be empty if no email address is found thus each line will either be blank or be populated with the email address. 每行有效,该模式由非捕获组中的两个模式
(?:
。第一个模式从行的开始.*?
开始,直到所有group1包含电子邮件模式(\\w[^@\\s]*@[^@\\s]{2,})
后跟其他任何.*
直到该行的末尾,第二个模式将匹配所有其他行而不发送电子邮件,这是搜索模式。替换模式为group1 $1
如果未找到电子邮件地址,则Group1将为空,因此每一行将为空或填充该电子邮件地址。
This might not match all email addresses but should match most. 这可能不匹配所有电子邮件地址,但应该匹配大多数。 See this question for a lengthy read about regex matching email addresses.
请参阅此问题 ,以获取有关正则表达式匹配电子邮件地址的详细信息。
You can't match everything but email. 除了电子邮件,您无法匹配所有内容。 But you can match everything and email.
但是您可以匹配所有内容和电子邮件。
Match anything non-greedily followed by captured email or end of string. 匹配所有非贪婪的内容,后跟捕获的电子邮件或字符串结尾。 Change to the capture group, globally:
全局更改为捕获组:
"BLAHBLAHemailBLAHBLAHemailBLAH".replace(/.*?(email|$)/g, "$1");
// => "emailemail"
(insert your own email regexp.) (插入您自己的电子邮件正则表达式。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.