简体   繁体   中英

Match Everything Except Email With This Regex Pattern

So, I have rows of text with plenty unnecessary information in them (in a google sheet). I would like to match everything except the EMAIL for which I'm using the following regex:

[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

However for Google Sheets you need to remove the ^ and $ line start/end markers and it should do most of what you want. So:

(?:.*?(\\w[^@\\s]*@[^@\\s]{2,}).*?|.+)

replace this pattern with $1 to leave just the email address per line

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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM