# How to see if string is true compared with a pattern

**URL:** <https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802>\
**Category:** Rails\
**Created:** [January 14, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802 "2007-01-14T15:53:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 14, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/1 "2007-01-14T15:53:45Z")

</div>

hi,

I have the following pattern to validate email address’s

^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$

how can I compare this with a string and return true or false?

thanks  
paul

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 14, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/2 "2007-01-14T16:21:36Z")

</div>

dont worry ive got it

pEmailRegEx =  
Regexp.new(/^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$/)  
@emailArray.delete\_if {|email| !pEmailRegEx.match(email)}

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 15, 2007, 4:08am UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/3 "2007-01-15T04:08:59Z")

</div>

paul wrote:

> dont worry ive got it
> 
> pEmailRegEx =  
> Regexp.new(/^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$/)  
> @emailArray.delete\_if {|email| !pEmailRegEx.match(email)}

You could do that, but it can be MUCH simpler than that

‘removed\_email\_address@domain.invalid’ =~ %r{some regex here}

returns true if it’s a match, or false if it’s not.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/4 "2007-01-19T16:29:21Z")

</div>

On Jan 14, 2007, at 15:53 , paul wrote:

> hi,
> 
> I have the following pattern to validate email address’s
> 
> ^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-  
> Z])\*.)+[a-zA-Z]{2,9})$
> 
> how can I compare this with a string and return true or false?

## ----------------------------------------------------------- String#match str.match(pattern) =\> matchdata or nil

```
  Converts _pattern_ to a +Regexp+ (if it isn't already one), then
  invokes its +match+ method on _str_.

     'hello'.match('(.)\1') #=> #<MatchData:0x401b3d30>
     'hello'.match('(.)\1')[0] #=> "ll"
     'hello'.match(/(.)\1/)[0] #=> "ll"
     'hello'.match('xx') #=> nil

```

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/5 "2007-01-19T16:29:52Z")

</div>

paul wrote:

> I have the following pattern to validate email address’s
> 
> ^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$
> 
> how can I compare this with a string and return true or false?

You can also do it Perl style:

email\_address =~  
/^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$/

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [September 25, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/how-to-see-if-string-is-true-compared-with-a-pattern/81802/6 "2007-09-25T22:34:12Z")

</div>

I wrote:

> email\_address =~  
> /^(0-9a-zA-Z_@(([0-9a-zA-Z])+([-\w]_[0-9a-zA-Z])\*.)+[a-zA-Z]{2,9})$/

That’s supposed to be on one line, dang word wrap.

–  
Michael W.
