Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex extract email from strings

I want to know if by using regular expressions I am able to extract emails from the following strings?

The following RE pattern is .*@.*match with all strings. It has worked fine with some of the string, though with not all.

I want to match all strings match with email pattern include all domain like (some-url.com) or (some-url.co.id)

boleh di kirim ke email saya [email protected] tks... boleh minta kirim ke [email protected].  [email protected]. . [email protected] Senior Quantity Surveyor [email protected], terimakasih bu Cindy Hartanto [email protected] saya mau dong bu cindy [email protected]  Hi Cindy ...pls share the Salary guide to [email protected] thank a 
like image 778
Cignitor Avatar asked Feb 23 '17 05:02

Cignitor


People also ask

How do I extract an email from a string?

Example of JavaScript extract email from string_-]+@[a-zA-Z0-9. _-]+\. [a-zA-Z0-9_-]+)/ to extract email ids (address) from the long text.

How do I extract an email from a string in Python?

To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.

What is the regex for email?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.


1 Answers

You can create a function with regex /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/ to extract email ids from long text

function extractEmails (text) {   return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi); } 

Script in action: Run to see result

var text = `boleh di kirim ke email saya [email protected] tks... boleh minta kirim ke [email protected]. [email protected]. .  [email protected] Senior Quantity Surveyor [email protected], terimakasih bu Cindy Hartanto [email protected] saya mau dong bu cindy [email protected]  Hi Cindy ...pls share the Salary guide to [email protected] thank a`;   function extractEmails ( text ){     return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);     }           $("#emails").text(extractEmails(text));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <p id="emails"></p>

While the regex in the above code snippet matches most email patterns, but if you still need to match >99% of the email patterns, including the edge cases (like '+' in the email) then use the regex pattern as shown below

Script in action: Run to see result

var text = `boleh di kirim ke email saya [email protected] tks... boleh minta kirim ke [email protected]. [email protected]. .  [email protected] Senior Quantity Surveyor [email protected], terimakasih bu Cindy Hartanto [email protected] saya mau dong bu cindy [email protected]  Hi Cindy ...pls share the Salary guide to [email protected] thank a`;   function extractEmails ( text ){     return text.match(/(?:[a-z0-9+!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi);     }           $("#emails").text(extractEmails(text));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <p id="emails"></p>
like image 85
Ambrish Pathak Avatar answered Sep 16 '22 21:09

Ambrish Pathak