How to get email address from a long string
If you're not sure which part of the space-separated string is the e-mail address, you can split the string by spaces and use
filter_var($email, FILTER_VALIDATE_EMAIL)
on each substring.
Extract email sub-strings from large document
This code extracts the email addresses in a string. Use it while reading line by line
>>> import re
>>> line = "should we use regex more often? let me know at jdsk@bob.com.lol"
>>> match = re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
>>> match.group(0)
'jdsk@bob.com.lol'
If you have several email addresses use findall
:
>>> line = "should we use regex more often? let me know at jdsk@bob.com.lol or popop@coco.com"
>>> match = re.findall(r'[\w.+-]+@[\w-]+\.[\w.-]+', line)
>>> match
['jdsk@bob.com.lol', 'popop@coco.com']
The regex above probably finds the most common non-fake email address. If you want to be completely aligned with the RFC 5322 you should check which email addresses follow the specification. Check this out to avoid any bugs in finding email addresses correctly.
Edit: as suggested in a comment by @kostek:
In the string Contact us at support@example.com.
my regex returns support@example.com. (with dot at the end). To avoid this, use [\w\.,]+@[\w\.,]+\.\w+)
Edit II: another wonderful improvement was mentioned in the comments: [\w\.-]+@[\w\.-]+\.\w+
which will capture example@do-main.com as well.
Edit III: Added further improvements as discussed in the comments: "In addition to allowing + in the beginning of the address, this also ensures that there is at least one period in the domain. It allows multiple segments of domain like abc.co.uk as well, and does NOT match bad@ss :). Finally, you don't actually need to escape periods within a character class, so it doesn't do that."
Extract email address from string - php
Try this
<?php
$string = 'Ruchika < ruchika@example.com >';
$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches[0]);
?>
see demo here
Second method
<?php
$text = 'Ruchika < ruchika@example.com >';
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $text, $matches);
print_r($matches[0]);
?>
See demo here
Extract email address from string
Here's a simple example showing how to use regex in JavaScript :
var string = "Francesco Renga <francesco_renga-001@gmail.com>"; // Your string containing
var regex = /<(.*)>/g; // The actual regex
var matches = regex.exec(string);
console.log(matches[1]);
regex extract email from strings
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 ekoprasetyo.crb@outlook.com tks... boleh minta kirim ke db.maulana@gmail.com. dee.wien@yahoo.com. .
deninainggolan@yahoo.co.id Senior Quantity Surveyor
Fajar.rohita@hotmail.com, terimakasih bu Cindy Hartanto
firmansyah1404@gmail.com saya mau dong bu cindy
fransiscajw@gmail.com
Hi Cindy ...pls share the Salary guide to donny_tri_wardono@yahoo.co.id 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>
How do I identify an email address from a long string and replace the email address with my company's email address?
The following code will replace all email strings that match the patern with your email id, the Regex
class can be found in System.Text.RegularExpressions
namespace
Regex emailReplace = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
emailReplace.Replace("YOUR TEXT HERE", "support@companyname.com");
How do you get an email address within a string
I pulled a few of the answers here into something like this. It actually returns each email address from the string (sometimes there are multiples from the mail host and target address). I can then match each of the email addresses up against the outbound addresses we sent, to verify. I used the article from @plinth to get a better understanding of the regular expression and modified the code from @Chris Bint
However, I'm still wondering if this is the fastest way to monitor 10,000+ emails? Are there any more efficient methods (while still using c#)? The live code won't recreate the Regex object every time within the loop.
public static MatchCollection CheckEmail(string email)
{
Regex regex = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(email);
return matches;
}
Related Topics
What Causes: "Notice: Uninitialized String Offset" to Appear
Casting an Array with Numeric Keys as an Object
Passing PHP Variable from Controller to JavaScript Function in the Head of a View
How to Add New Column to MySQL Table
How to Generate a Random Key Within PHP
How to Use Prepared Statements in Codeigniter
Check If Specific Array Key Exists in Multidimensional Array - PHP
Access Get Directly from JavaScript
Check for Consecutive Dates Within a Set and Return as Range
How to Store Null Values in Datetime Fields in MySQL
How to Create a Psr-4 Autoloader for My Project
How to Capture the Result of Var_Dump to a String
Correct Way to Set Bearer Token with Curl
Array and String Offset Access Syntax with Curly Braces Is Deprecated