44275

regex extract email from strings

Question:

i want to know if regex is able to extract the email from the following strings ?

the following condition is .*@.*match with all string. to some strings, my pattern is worked, but the rest of them are not.

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

Answer1:

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

<pre class="snippet-code-js lang-js prettyprint-override">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)); <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <p id="emails">

Answer2:

[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+ worked for me, you can check the result on this <a href="https://www.regex101.com/r/KIbf1T/1" rel="nofollow">regex101 saved regex</a>.

It's really just twice the same pattern separated by an @ sign.

The pattern is 1 or more occurences of:

<ul><li>a-z: any lowercase letter</li> <li>A-Z: any uppercase letter</li> <li>0-9: any digit</li> <li>-_.: a hyphen, an underscore or a dot</li> </ul>

If it missed some emails, add any missing character to it and it should do the trick.

<strong>Edit</strong>

I didn't notice it first, but when going to the <a href="https://www.regex101.com/r/KIbf1T/1" rel="nofollow">regex101 link</a>, there's an <em>Explanation</em> section at the top-right corner of the screen explaining what the regular expression matches.

Answer3:

You can use the following <em>regex</em> to capture all the email addresses.

(?<name>[\w.]+)\@(?<domain>\w+\.\w+)(\.\w+)?

see <a href="https://regex101.com/r/hh6qeF/3" rel="nofollow">demo / explanation</a>

additionally if you want, you can capture only those emails that contains a specific domain name <em>(ie. some-url.com)</em> and to achieve that you just need to replace the \w+\.\w+ part after <domain> with your desired domain name. so, it would be like (?<name>[\w.]+)\@(?<domain>outlook.com)(\.\w+)?

see <a href="https://regex101.com/r/hh6qeF/4" rel="nofollow">demo / explanation</a>

Answer4:

Hope this will work.

[\w\.]+\@[\w]+(?:\.[\w]{3}|\.[\w]{2}\.[\w]{2})\b

<a href="https://regex101.com/r/VsGFWz/1" rel="nofollow">Regex Demo</a>

Answer5:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}+\.[A-Z]{2,}

Recommend

  • Remove dynamically inserted code
  • Rails 4 - incorporating vendor assets
  • Handling multiple forms and printing content [closed]
  • jqGrid cellattr title ignores text after white spaces
  • Change imported Dll name?
  • Finding Multiple text in Excel
  • matching parentheses in python regular expression
  • How to find specific org/slf4j/Logger jar file out of multiple bindings from the apache zip?
  • Why would my AJAX script not pass variables to PHP to pass into my database?
  • How to return value from service to controller in Angularjs
  • Create Directory in Swift 3.0
  • android - Installed app section shows package name instead of app name for my own application
  • Getting an error(MESSAGE A handler conflict occurred. This may disable some commands) when launching
  • Reading a user-input string in prolog
  • get the creation time from core dump
  • How do I run an Oracle stored procedure in Ruby
  • play a stream in iTunes without adding it to the library / playlist
  • not able to give image to button
  • How to use table of number in SELECT INTO Statment
  • php upload multiple files via ftp
  • Display dynamically generated image to the browser using jsp
  • iphone blue tooth connection
  • Access R Shiny application not with an IP
  • playback audio file from byte code (audio file format is CCITT A-Law)
  • Beginner sql join query
  • How to run windows start service using inno setup?
  • Single-row subquery returns more than one row
  • How do I give each person in this list a raise
  • Python EOF error when reading input
  • django cms doesn't see static folder
  • How to filter a range by first Character in a cell?
  • Using DOM parser in Android
  • How do I add conditionally to a selection of cells in a pandas dataframe column when the the column
  • PL/SQL on SquirreL SQL Client 3.7.1
  • Getting data from database using HQL
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Is there a way to save the selected text and highlight it again once the page is refreshed?
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • How would I use PHP exceptions to define a redirect?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app