필터 지우기
필터 지우기

Extract email addresses from text

조회 수: 6 (최근 30일)
Gustav Essunger
Gustav Essunger 2017년 6월 8일
댓글: Adam Danz 2021년 6월 2일
Hello everyone!
Does anyone have a script (or know how to create one) that extracts email addresses from a text string?
(With similar function as this website: http://www.procato.com/mailextract/)
Thanks in advance! Gustav
  댓글 수: 1
Steven Lord
Steven Lord 2017년 6월 9일
It's likely more complicated than you think. See for example this Microsoft blog and this Stack Overflow question. One of the answers on the Stack Overflow page links to a (five year old) page giving a regular expression that I suspect you could use with the regular expression functionality in MATLAB.

댓글을 달려면 로그인하십시오.

답변 (2개)

Stephen23
Stephen23 2017년 6월 8일
편집: Stephen23 2021년 6월 2일
Take MATLAB's own Regular Expressions example:
email = '[a-z_]+@[a-z]+\.(com|net)';
and adapt it to allow any domain, or whatever other requirements you have:
rgx = '[a-z0-9_]+@[a-z0-9]+(\.[a-z0-9]+)+';
C = regexpi(txt,rgx,'match');
For a slightly stricter version, you can find many regular expressions on the internet, e.g.:
rgx = '[a-z0-9._%+-]+@[a-z0-9-]+(\.[a-z0-9-]+)+'
While this simple regular expression works for simple email adresses, it is worth noting that the complete rules for checking valid email adresses are not trivial to implement with a regular expression:
A common mistake (including by this answer) is to exclude non-latin characters.
  댓글 수: 2
Gustav Essunger
Gustav Essunger 2017년 6월 9일
Thank you very much!!
Adam Danz
Adam Danz 2021년 6월 2일
Don't forget Mailojis! 😁

댓글을 달려면 로그인하십시오.


oliver
oliver 2019년 2월 15일
편집: oliver 2019년 2월 15일
I think the above examples will miss quite a lot of emails, like all those containing a capital letter or things like: Peter.O'Toole@xyz.com. So my suggestion would be something like:
reg='[a-zA-Z0-9._%''+-]+@([a-zA-Z0-9._-])+\.([a-zA-Z]{2,4})';
Although with the wide variety of new TLDs nowadays, limiting the last character group to 2-4 letters may be obsolete (depending on your needs).
  댓글 수: 1
Daniele Lupo
Daniele Lupo 2021년 6월 2일
This regexp validates an invalid mail with consecutive dots, like "my.email@email...com".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by