Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to build a method to validate emails
- def email_is_junk(email_address)
- end
- USER_RULES = ['+', 'do-not-reply', 'support', 'test', 'service', 'tips', 'twitter', 'alerts', 'survey']
- DOMAIN_RULES = ['craigslist.org']
- def email_is_junk(email)
- return true if !email.match('@') # return early if no @
- user, domain = email.split('@')
- USER_RULES.each { |rule| return true if user.match(rule) }
- DOMAIN_RULES.each { |rule| return true if domain.match(rule) }
- false # reached the end without matching anything
- end
- Regexp.union #=> /(?!)/
- Regexp.union("penzance") #=> /penzance/
- Regexp.union("a+b*c") #=> /a+b*c/
- Regexp.union("skiing", "sledding") #=> /skiing|sledding/
- Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
- Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
- Regexp.escape('*?{}.') #=> \*?{}.
- patterns = [
- /.+?+.+?@/
- ]
- strings = [
- 'do-not-reply', 'support', 'test', 'service', 'tips', 'twitter', 'alerts', 'survey',
- 'craigslist.org'
- ]
- regex = Regexp.union(
- *patterns,
- *strings.map{ |s|
- Regexp.new( Regexp.escape("#{ s }@"), Regexp::IGNORECASE ) }
- )
- pp regex
- >> /(?-mix:.+?+.+?@)|(?i-mx:do-not-reply@)|(?i-mx:support@)|(?i-mx:test@)|(?i-mx:service@)|(?i-mx:tips@)|(?i-mx:twitter@)|(?i-mx:alerts@)|(?i-mx:survey@)|(?i-mx
- sample_email_addresses = %w[
- user
- user+foo
- do-not-reply
- support
- service
- tips
- twitter
- alerts
- survey
- ].map{ |e| e << '@host.com' }
- pp sample_email_addresses.map{ |e| [e, !!e[regex]] }
- >> [["user@host.com", false],
- >> ["user+foo@host.com", true],
- >> ["do-not-reply@host.com", true],
- >> ["support@host.com", true],
- >> ["service@host.com", true],
- >> ["tips@host.com", true],
- >> ["twitter@host.com", true],
- >> ["alerts@host.com", true],
- >> ["survey@host.com", true]]
- pp sample_email_addresses.select{ |e| e[regex] }
- >> ["user+foo@host.com",
- >> "do-not-reply@host.com",
- >> "support@host.com",
- >> "service@host.com",
- >> "tips@host.com",
- >> "twitter@host.com",
- >> "alerts@host.com",
- >> "survey@host.com"]
- pp sample_email_addresses.reject{ |e| e[regex] }
- >> ["user@host.com"]
- function isJunk(email) {
- return hasPlus(email) || supportLike(email) || craigsList(email);
- }
- function craigsList(email) {
- return email.match(/@craigslist.org/);
- }
- function supportLike(email) {
- return email.match(/do-not-reply|support|test|service|tips|twitter|alerts|survey/);
- }
- function hasPlus(email) {
- return email.match(/+.*@/);
- }
Add Comment
Please, Sign In to add comment