My program takes an input from Slack. When an email is provided, Slack automatically converts it to a mail to tag:
<mailto:timxxxx@gmail.com|timxxxx@gmail.com>
The email addresses are the same.
I want to easily extract the email IF it is provided in this form, but if the email comes through another channel where it hasn't been reformatted, take the email.
答案 0 :(得分:0)
您没有指定其他格式,但您可以使用正则表达式来识别给定格式的字符串,并验证|
的两边确实是同一个地址。代码类似于:
def get_mail(str)
matches = /<mailto:([\w\-\.]+@[\w\-\.]+)\|([\w\-\.]+@[\w\-\.]+)>/.match(str)
if (matches.nil? || matches[0] != matches[1])
str
else
matches[0]
end
end
请注意,某些特殊情况的电子邮件地址可能无法匹配,因为有些疯狂的电子邮件是有效的,但这适用于基本情况。如果需要,您可以修改正则表达式以使用更具包容性的电子邮件匹配。
答案 1 :(得分:0)
我假设如果字符串如下:
<mailto:....>
这些点代表一个或多个电子邮件地址,如果不止一个,则它们由"|"
分隔。此外,我假设不需要检查字符串的一部分以确认它包含有效的电子邮件地址,并正确分开。在这些假设是正确的,您不需要正则表达式来提取电子邮件地址:
def extract_addresses(str)
return nil unless str.start_with?('<mailto:') and str.end_with?('>')
str[8..-2].split('|')
end
addresses = extract_addresses "<mailto:timxxxx@gmail.com|timxxxx@gmail.com>"
#=> ["timxxxx@gmail.com", "timxxxx@gmail.com"]
puts addresses ? "mailto format" : "not mailto format"
mailto format
puts "addresses are the same" if addresses && addresses.uniq.size == 1
addresses are the same
addresses = extract_addresses "<the quick brown dog jumped over the lazy fox>"
#=> nil