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.
paul (Guest)
on 2007-01-14 16: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
paul (Guest)
on 2007-01-14 17: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)}
Alex W. (Guest)
on 2007-01-15 05: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

  'removed_email_address@domain.invalid' =~ %r{some regex here}

returns true if it's a match, or false if it's not.
Jakob S. (Guest)
on 2007-01-19 17: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 S. - http://mentalized.net
Michael W. (Guest)
on 2007-01-19 17: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 W.
Michael W. (Guest)
on 2007-09-26 00: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 W.
This topic is locked and can not be replied to.