Validating with regular expressions

In various use-cases, but especially at web-based registration forms we need to make sure the value we received is a valid e-mail address.Another common use-case is when we get a large text-file (a dump, or a log file) and we need to extract the list of e-mail address from that file.

validating with regular expressions-28

Before we try to answer that question, let me point out that there are already, ready-made and high-quality solutions for these problems.

Email:: Address can be used to extract a list of e-mail addresses from a given string.

For example: examples/email_It properly verifies if an e-mail is valid, it even removes unnecessary white-spaces from both ends of the e-mail address, but it cannot really verify if the given e-mail address is really the address of someone, and if that someone is the same person who typed it in, in a registration form.

These can be verified only by actually sending an e-mail to that address with a code and asking the user there to verify that indeed s/he wanted to subscribe, or do whatever action triggered the email validation.

With that said, there might be cases when you cannot use those modules and you'd like to implement your own solution using regular expressions.

One of the best (and maybe only valid) use-cases is when you would like to teach regexes.RFC822 specifies how an e-mail address must look like but we know that e-mail addresses look like this: where the "username" part can contain letters, numbers, dots; the "domain" part can contain letters, numbers, dashes, dots.Actually there are a number of additional possibilities and additional limitations, but this is a good start describing an e-mail address.I am not really sure if there are length limitation on either of the username or the domain name.Because we will want to make sure the given string matches exactly our regex, we start with an anchor matching the beginning of the string We can use all lower-case characters as the e-mail addresses are case sensitive.We just have to make sure that when we try to validate an e-mail address first we'll convert the string to lower-case letters.

Tags: , ,