How to see if string is true compared with a pattern


#1

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


#2

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)}


#3

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.


#4

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


#5

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.


#6

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.