# Scan string w regex .. and HNYTAOY!

**URL:** <https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134>\
**Category:** Ruby\
**Created:** [January 4, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134 "2007-01-04T12:50:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/1 "2007-01-04T12:50:32Z")

</div>

HNYTAOY (Dry version of … Happy New Year To All Of You)

I am trying to get an array of email addresses from a string , as this  
string is coming from an input textarea in a from, even if I ask for a  
blank between each address, users will also enter some return  
characters…

so I get something like :

to\_string =  
“removed\_email\_address@domain.invalid\r\removed\_email\_address@domain.invalid\r\removed\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid”

I know how to use a\_string.scan(" "), but here I should use a regex…  
after many try … could not find the correct one…  
could someone make 2007 starting on right tracks… thanks a lot !!

joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/2 "2007-01-04T12:56:32Z")

</div>

On 2007-01-04 12:47:06 +0100, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> “removed\_email\_address@domain.invalid\r\removed\_email\_address@domain.invalid\r\removed\_email\_address@domain.invalid  
> removed\_email\_address@domain.invalid”
> 
> I know how to use a\_string.scan(" "), but here I should use a regex…  
> after many try … could not find the correct one…  
> could someone make 2007 starting on right tracks… thanks a lot !!
> 
> joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/3 "2007-01-04T13:08:36Z")

</div>

On Jan 4, 2007, at 6:55 AM, Josselin wrote:

> > I know how to use a\_string.scan(" "), but here I should use a  
> > regex… after many try … could not find the correct one…  
> > could someone make 2007 starting on right tracks… thanks a lot !!  
> > joss
> 
> forgot to mention what I wwrote until now :
> 
> > to\_string.split(%r{\r\n\s\*}) which gives me teh addresses  
> > separated by \r\n , but I missed the addresses separated by spaces

I would just scan for email addresses. I got this:  
to\_string.scan(/[A-Z0-9.\_%-]+@[A-Z0-9.-]+.[A-Z]{2,4}/i)

From this site:  
[http://www.regular-expressions.info/email.html](http://www.regular-expressions.info/email.html)

Which seems to do the job reasonably well.  
Happy new year to you too!  
-Mat

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/4 "2007-01-04T13:36:43Z")

</div>

On 2007-01-04 13:07:32 +0100, Mat S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > > so I get something like :  
> > > by \r\n , but I missed the addresses separated by spaces  
> > > -Mat  
> > > thanks Mat… I got it too… my concern is getting all adresses in one  
> > > array…  
> > > It seems that I could do a scan first to replace the \r\n by a space  
> > > then doing a split on space character will give me the array  
> > > finally I’ll use the regex to chexk each email address ( I am using a  
> > > Rails plugin validates\_as\_email , based on RFC2822 with a possibility  
> > > to perform an online checking if needed)

Joss

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/5 "2007-01-04T13:40:45Z")

</div>

On 04.01.2007 13:07, Mat S. wrote:

> On Jan 4, 2007, at 6:55 AM, Josselin wrote:
> 
> > forgot to mention what I wwrote until now :
> > 
> > > to\_string.split(%r{\r\n\s\*}) which gives me teh addresses separated  
> > > by \r\n , but I missed the addresses separated by spaces
> 
> I would just scan for email addresses.

Yeah, that’s what I’d also prefer over splitting.

> I got this:  
> to\_string.scan(/[A-Z0-9.\_%-]+@[A-Z0-9.-]+.[A-Z]{2,4}/i)
> 
> From this site:  
> [How to Find or Validate an Email Address](http://www.regular-expressions.info/email.html)
> 
> Which seems to do the job reasonably well.

Hm, IMHO the domain part could be a bit better with respect to dots.  
Something like

(?:[A-Z0-9-]+.)+[A-Z]{2,4}

In some circumstances something simple like this will even work

[^\s@]+@[^\s@]+

And I bet there is a ton of variants…

Cheers

```
robert

```

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/6 "2007-01-04T14:26:54Z")

</div>

On 1/4/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to\_string.split
> 
> That seems easier than a regex to match an email.
> 
> James Edward G. II

That’s definitely easier. And more certain.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/7 "2007-01-04T14:06:48Z")

</div>

On Jan 4, 2007, at 5:55 AM, Josselin wrote:

> forgot to mention what I wwrote until now :
> 
> > to\_string.split(%r{\r\n\s\*}) which gives me teh addresses  
> > separated by \r\n , but I missed the addresses separated by spaces

You can split() on any whitespace characters with:

to\_string.split

That seems easier than a regex to match an email.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 4, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/scan-string-w-regex-and-hnytaoy/81134/8 "2007-01-04T14:46:55Z")

</div>

On Jan 4, 2007, at 8:06 AM, James Edward G. II wrote:

> That seems easier than a regex to match an email.

/me slaps his forehead

agreed…  
-Mat
