regex Help

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

looking for regex pattern for validating emailid
emailid can have a-z 0-9 - _ . (a to z, 0 ot 9, hyphen,undercore, dot)


here is a sample which I got from net which doesnt allow hyphen(-)

string
pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

Modified the same t o

string
pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)*([-][a-z|0-9]+([.][a-z|0-9]+([_][a-z|0-9]+)*)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";


it works only for one hyphen (-)

Whats wrong here..

thanks in advance
praveen
 
Try this:

pattern=@"^[a-z][a-z|0-9|\-|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|
0-9|\-]+)*)?@[a-z][a-z|0-9|\-]*\.([a-z][a-z|0-9|\-]*(\.[a-z][a-z|0-9|
\-]*)?)$";

The problem with your extension is, that you allow the hyphen to be
there only once because you have no quantifier specified.
No quantifier means the character has to be there exactly once.

regards, piccante
 
thanks,
It works for muliple hyphens,
but wont work for various combination of hyphens,underscore,dots like
following

(e-mail address removed)
(e-mail address removed)


need any number of hyphes,dots,underscores allowed

thanks,
praveen
 
Hello praveen,
looking for regex pattern for validating emailid
emailid can have a-z 0-9 - _ . (a to z, 0 ot 9, hyphen,undercore,
dot)
here is a sample which I got from net which doesnt allow hyphen(-)

string
pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]
+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

Modified the same t o

string
pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]
+)*)*([-][a-z|0-9]+([.][a-z|0-9]+([_][a-z|0-9]+)*)*)?@[a-z][a-z|0-9|]*
\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

it works only for one hyphen (-)

Whats wrong here..

One thing to note is that you shoudn't burn your hands on the perfect regular
expression for a correct emailaddress. If you look at the specs, the possibiities
are endless.

If you do want to take a stab at it, make sure you define a good rule, to
which the address should conform.

One I've seen used, and quite like is

- it begins with a letter
- followed by either a repeating group of
- a group of letters and numbers
- or a -, _, . followed by a group of letters and numbers
- followed by a @
- followed by a genuine domain name
- a letter or a number
- followed either by a repeating group of
- a group of letters and numbers
- or a -, _, . followed by a group of letters and numbers
- followed by a group of
- more than 2 letters


which is easily translated to a regex:

[a-z]([a-z0-9]|[-_.][a-z0-9])+@[a-z]([a-z0-9]|[-_.][a-z0-9])+\.[a-z]+

Keep in mind that this does not account for the new unicode domain names,
email addresses formatted as "your full name"@domainname, [email protected]
and such which are all valid email addresses.

The best way to verify if an email address is correct is trying to send it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top