简体   繁体   中英

Extract email addresses from a block of text

How can I create an array of email addresses contained within a block of text? I've tried

addrs = text.scan(/ .+?@.+? /).map{|e| e[1...-1]}

but (not surprisingly) it doesn't work reliably.

Howabout this for a (slightly) better regular expression

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

You can find this here:

Email Regex

Just an FYI, the problem with your email is that you allow only one type of separator before or after an email address. You would match "@" alone, if separated by spaces.

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