Forum: Ruby on Rails how to see if string is true compared with a pattern

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3a0257019a40dbaf6cbda0cc780c1720?d=identicon&s=25 paul (Guest)
on 2007-01-14 15:53
hi,

I have the following pattern to validate email address's

^([0-9a-zA-Z]([-.\w]*[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
3a0257019a40dbaf6cbda0cc780c1720?d=identicon&s=25 paul (Guest)
on 2007-01-14 16:21
dont worry ive got it

pEmailRegEx =
Regexp.new(/^([0-9a-zA-Z]([-.\w]*[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)}
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-01-15 04:08
paul wrote:
> dont worry ive got it
>
> pEmailRegEx =
> 
Regexp.new(/^([0-9a-zA-Z]([-.\w]*[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

  'foo@bar.com' =~ %r{some regex here}

returns true if it's a match, or false if it's not.
317aad7f9f0b69a440faca74bbe22b20?d=identicon&s=25 Jakob Skjerning (Guest)
on 2007-01-19 16:29
(Received via mailing list)
On Jan 14, 2007, at 15:53 , paul wrote:

> hi,
>
> I have the following pattern to validate email address's
>
> ^([0-9a-zA-Z]([-.\w]*[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 Skjerning - http://mentalized.net
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-01-19 16:29
(Received via mailing list)
paul wrote:
> I have the following pattern to validate email address's
>
> 
^([0-9a-zA-Z]([-.\w]*[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]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/


--
Michael Wang
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-09-25 22:34
(Received via mailing list)
I wrote:
> email_address =~
> 
/^([0-9a-zA-Z]([-.\w]*[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 Wang
This topic is locked and can not be replied to.