从一个文本块中提取电子邮件地址

时间:2022-10-23 19:23:53

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

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

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

但是(毫不奇怪)它不能可靠地工作。

1 个解决方案

#1


9  

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.

仅仅是一个FYI,您的电子邮件的问题是您在电子邮件地址之前或之后只允许一种类型的分隔符。如果用空格分隔,您将单独匹配“@”。

#1


9  

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.

仅仅是一个FYI,您的电子邮件的问题是您在电子邮件地址之前或之后只允许一种类型的分隔符。如果用空格分隔,您将单独匹配“@”。

相关文章