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.
Strings are used for storing text/characters. For example, "Hello World" is a string of characters.
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.
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.
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" ).
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).
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With