" When I do t...">

Simple - using String # scan to retrieve an email address

I have a line containing:

@from = "John Doe <john.doe@daemon.co.uk>"

When I do this:

@from.scan('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i')

I am not getting any results. I am trying to extract the email address myself.

I tried to delete \ b, but that didn't work either.

Any help would be greatly appreciated.

+5
source share
3 answers

Your expression works just fine: rubular

The problem is that the quotation marks around your regular expression mean that it is interpreted as a simple text string, not a regular expression. Removing quotes solves the problem: ideone

@from = "John Doe <john.doe@daemon.co.uk>"
@from.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i) { |x| puts x } 

Conclusion:

john.doe@daemon.co.uk
+18
source

, "John Doe <john.doe@daemon.co.uk>", .

Ruby Mail::Address.

require 'mail'
#=> true
a = Mail::Address.new("John Doe <john.doe@daemon.co.uk>")
#=> #<Mail::Address:70264542184500 Address: |John Doe <john.doe@daemon.co.uk>| >
a.address
#=> "john.doe@daemon.co.uk"
a.display_name
#=> "John Doe"
+9

, , :

: TLD 4. TLD , , :

@from.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i)

, , 4 , 4 TLD. TLD, , 2, 3 4 (.com,.org,.info ..). (.auction,.software,.business ..).

, TLD ( 2 - ).

+6
source

All Articles