# Fetch string matching with email

**URL:** <https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623>\
**Category:** Rails\
**Created:** [August 21, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623 "2007-08-21T15:17:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 21, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/1 "2007-08-21T15:17:12Z")

</div>

hello frnds

I want to match and fetch the string from the db contents so is there a  
method for it.

The contents can be removed\_email\_address@domain.invalid or also \<a href mailto:removed\_email\_address@domain.invalid  
from both the things I want to fetch only the email id i.e removed\_email\_address@domain.invalid

Can ne one help me out?

Thanks

Dhaval P.

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 21, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/2 "2007-08-21T19:59:29Z")

</div>

Hi Dhaval,

data = "you data " (can be removed\_email\_address@domain.invalid or

[ 

you do this

 

regex = /[:]?[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]{3}

 

if data =~ regex  
puts “matched String:(email): #{$&}”  
else  
puts “no match”  
end

 

$& contains the matched sting, $’ : contains the post string after  
match, $`: contains the pre string that precedes your match criteria

 

P.s: change the regex value to what ever you want as necessary.

 ](mailto:removed_email_address@domain.invalid)

[On Aug 21, 6:17 pm, Dhaval P.](mailto:removed_email_address@domain.invalid)[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 22, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/3 "2007-08-22T13:37:25Z")

</div>

ok thanks but what if I want to use the samething with for loop then  
will it work?

thanks a lot

Dhaval P.

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 6, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/4 "2007-09-06T12:31:16Z")

</div>

I am currently using .match(/\S+@\S+.\S+/) but i want to refine  
it…for the last string i.e after . i want to restrict the no of char  
to max 3 only the others should not be displayed …can ne one tell me  
how to do it.

thanks

dhaval parikh

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 7, 2007, 6:38am UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/5 "2007-09-07T06:38:54Z")

</div>

.match(/\S+@\S+.[a-zA-Z0-9]{3,3}/) (min number of char = 3 and max  
number of char = 3)  
.match(/\S+@\S+.[a-zA-Z0-9]{3}/) (min number of char = 3 and max  
number of char = unlimited)  
.match(/\S+@\S+.[a-zA-Z0-9]{,3}/) (min number of char = 0 and max  
number of char = 3)

On Sep 6, 3:31 pm, Dhaval P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 7, 2007, 9:43am UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/6 "2007-09-07T09:43:10Z")

</div>

I you were not already, I want to make you aware that there are top  
level domains with names longer than three characters. “museum” for  
instance.

Wikipedia has the whole list:

> **[List of Internet top-level domains](https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains)**
>
> This list of Internet top-level domains (TLD) contains top-level domains, which are those domains in the DNS root zone of the Domain Name System of the Internet. A list of the top-level domains by the Internet Assigned Numbers Authority (IANA) is maintained at the Root Zone Database. IANA also oversees the approval process for new proposed top-level domains for ICANN. As of April 2021\[update\], their root domain contains 1502 top-level domains. As of March 2021\[update\], the IANA root dat IANA...

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [September 7, 2007, 11:45am UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/7 "2007-09-07T11:45:52Z")

</div>

thanks a lot to both of you i modified the code a bit as per your  
suggestions

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 22, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/fetch-string-matching-with-email/112623/8 "2007-08-22T17:05:40Z")

</div>

sure it will work, the value of data variable is going to be what ever  
you want

example  
array\_of\_objects = [“[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)”, “removed\_email\_address@domain.invalid”, …]

array\_of\_objects.delete\_if { |data|  
!(data =~ regex)  
}

On Aug 22, 4:37 pm, Dhaval P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
