views:

78

answers:

2

Hi mates,

Really got stuck on this simple regex. Need it to validate a string, that will be a mail without the "@domain.xxx".

It must accomplish the following rules:

  • there must be a string (only letters) starting.
  • there must be a string (only letters) ending.
  • this two strings must be separated by a dot.
  • the complete string mustn't contain any numbers or simbols.

I was trying with something like... /^[a-z]+$/^[.]+$/[a-z]+$/i ...but no success.

+2  A: 
/^[A-Za-z]+\.[A-Za-z]+$/

will work for ASCII letters.

If you also want to allow international letters (äá etc.) try

/^[^\W\d_]+\.[^\W\d_]+$/

[^\W\d_] means "Any character that not a non-alphanumeric character, not a number and not an underscore".

Tim Pietzcker
`\w` matches *alphanumerics*, so to get alpha only, use `[^\W\d_]` or `[[:alpha:]]`.
Greg Bacon
Ah right, I was too hasty...will edit.
Tim Pietzcker
According to the RFC specifications you should probably also consider characters like !, #, and $. http://en.wikipedia.org/wiki/Email_address#RFC_specification
Jan Aagaard
Nice, that's working now. Thanks Tim. :)I'm hating regexp for a while. :P
Raul Illana
Check http://www.regular-expressions.info for a great tutorial. It's not so bad once you get the hang of it :)
Tim Pietzcker
Will do when worktime let's me. Thanks again. :)
Raul Illana
@Jan: Will consider nothing my boss hasn't asked to do. :)
Raul Illana
A: 

/^[a-zA-Z]+\.[a-zA-Z]+$/?

MBO
This would not allow upper case letters.
Jan Aagaard
You're wrong Jan. It's ok too. I've said nothing about uc or lc. ;)
Raul Illana
Sorry, MBO. It too late to change my vote. :)
Jan Aagaard
@Jan No problem, I can live with it :-) I created that regexp only to show what @Raul missed while creating his version. To add case insensitivity he can add `/i` flag or modify it as shown in another answer.
MBO