[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression

Johnathan Smith

1/13/2008 11:08:00 PM

hey

im looking to write a regular expression for an email address
however, i want the first part of the name to be initials (first initial
and optional middle initial) and a surname

such as

j.m.smith@domain.com

any help would be greatly appreciated

thanks
--
Posted via http://www.ruby-....

2 Answers

Herb Martin

1/13/2008 11:42:00 PM

0


"Johnathan Smith" <stu_09@hotmail.com> wrote in message
news:e431f7068f013e21119f070d48f79bf4@ruby-forum.com...
> hey
>
> im looking to write a regular expression for an email address
> however, i want the first part of the name to be initials (first initial
> and optional middle initial) and a surname
>
> such as
>
> j.m.smith@domain.com
>
> any help would be greatly appreciated

A naive attempt for your specific example might look something like
(untested):

/[a-zA-Z]\.[a-zA-Z]\.[a-zA-Z]\w+\@\w\.\w+/

BUT there are all sors of problems with this and it is likely doing
you a disservice for me to even suggest it might be sufficient or
correct.

Writing a general regular expression for all legal email addresses is
an surprisingly complex job. You should see the discussion in
"Mastering Regular Expressions" by Jeffrey E. F. Friedl (O'Reilly)
which is now in it's THIRD edition.

Technically the email, or rather RFC 2822 address is quite complicated
in all of it's possibilities and the following Google searches or links
might
be useful:

[ "regular expression" "email address" ]

My first hit was http://www.regular-expressions.info/...
Which offers serious discussion and suggests:

/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i

Another suggestion from http://regexlib.com/DisplayPat... is:
/^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$/


--
Herb Martin


Thomas Adam

1/14/2008 12:09:00 AM

0

Hi --

On 13/01/2008, Johnathan Smith <stu_09@hotmail.com> wrote:
> hey
>
> im looking to write a regular expression for an email address
> however, i want the first part of the name to be initials (first initial
> and optional middle initial) and a surname
>
> such as
>
> j.m.smith@domain.com
>
> any help would be greatly appreciated

What is it you want to do with this?

a="j.m.smith@domain.com"
p "yes" if a.scan(/\w+\.(\w+\.)??(\w+)??\@(\w+)(.*)/).size > 0

And several other variations thereof.

-- Thomas Adam