Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple - using String#scan to extract an email address

I've got a string that contains:

@from = "John Doe <[email protected]>"

When I do:

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

I get no results. I'm trying to extract the email address on it's own.

I tried removing the \b's but this did not work either.

Any help would be much appreciated.

like image 631
cjm2671 Avatar asked Nov 21 '11 22:11

cjm2671


People also ask

What is string with an example?

Strings are used for storing text/characters. For example, "Hello World" is a string of characters.

What is a string simple?

A string is any series of characters that are interpreted literally by a script. For example, "hello world" and "LKJH019283" are both examples of strings. In computer programming, a string is attached to a variable as shown in the example below.

What is the use of string?

String is used to tie, bind, or hang other objects. It is also used as a material to make things, such as textiles, and in arts and crafts. String is a simple tool, and its use by humans is known to have been developed tens of thousands of years ago.

What is an example of a string value?

A string value is just a sequence of characters, like "abc" . A string value is always enclosed in quotes. All types of characters are allowed (even digits, as in "abc123" ).


2 Answers

Sorry, I don't have enough rep to comment, so I'll make this an answer:

For any future use, everyone should make one modification: Don't restrict the TLD length to 4. New TLDs are being introduced very rapidly, you should now use a regex like this:

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

All I did was remove the 4 at the end of the regex, which places a maximum length of 4 characters on the TLD. TLDs used to almost all be 2, 3, or 4 characters long (.com, .org, .info, etc.). But now, they are introducing tons of new ones (.auction, .software, .business, etc.)

So nobody should restrict TLD length anymore (although leaving a minimum of 2 chars is still good).

like image 59
Kyletns Avatar answered Sep 18 '22 18:09

Kyletns


Your expression works fine: rubular

The problem is the quotes around your regular expression means that it is interpreted as a plain text string rather than a regular expression. Removing the quotes solves the problem: ideone

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

Output:

[email protected]
like image 20
Mark Byers Avatar answered Sep 18 '22 18:09

Mark Byers