Regular Expressions: An Introduction
by Amit Arora
Published: January 12, 2004
User Rating: 8.9 (274 votes)

Page Page » 1 2 3 4 5 6 7 8 9

Sub-patterns

As mentioned earlier, Sub-patterns are patterns within patterns. Sub-patterns are defined by putting the pattern in simple brackets ( )

So, whatever we have discussed for the patterns, is valid for sub-patterns. And yes, that means that sub-patterns can themselves have more nested sub-patterns.

Using sub-patterns, you can design very powerful regexps for the matching.

To sum up what we have discussed in this article, we will design a regular expression for matching a valid email address. For our test, we would assume that a valid email address

  1. Must start with an alphabet
  2. Followed by alphanumeric, including underscore (_), dash (-) and dot (.)
  3. Followed by the at the rate (@)
  4. Followed by alphanumeric, including dash(-)
  5. Followed by dot (.)
  6. Followed by top level domain (i.e. alphabet with minimum 2 and maximum 4 characters)
  7. Should be case-insensitive
  8. Should only match the above rules when the email address is NOT EMPTY

even though real email addresses may differ from this.

So, putting, everthing in action, we would be going step by step. Each steps pattern has been made bold so that you can understand clearly.

Step 1 Regex /^[a-zA-Z]$/ Explaintaion Start the email address by a alphabet Step 2 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*$/ Explaintaion Followed by alphanumeric, including underscore (_), dash (-) and dot (.) Step 3 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*\@$/ Explaintaion Followed by at the rate (@) Step 4 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*\@[a-z0-9\-]*$/ Explaintaion Followed by alphanumeric, including dash(-) Step 5 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*\@[a-zA-Z0-9\-]*\.$/ Explaintaion Followed by dot (.) Step 6 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*\@[a-zA-Z0-9\-]*\.[a-zA-Z]{2,4}$/ Explaintaion Followed by top level domain (i.e. alphabet with minimum 2 and maximum 4 characters) Step 7 Regex /^[a-zA-Z][a-zA-Z0-9\_\-\.]*\@[a-zA-Z0-9\-]*\.[a-zA-Z]{2,4}$/i Explaintaion Make the pattern case-insesitive Step 8 Regex /^([a-zA-Z][a-zA-Z0-9\_\-\.]*\@[a-zA-Z0-9\-]*\.[a-zA-Z]{2,4})?$/i Explaintaion Should only match the above rules when the email address is NOT EMPTY This has been done by putting the while pattern in a sub-pattern, and them matching that sub-pattern 0 or 1 times i.e matching only when there is atleast one character i.e matching when the email address is NOT EMPTY

Please note that there are better ways to define an regexp for matching the valid email address than what has been listed above, but using the above method, you would have learnt everything

Page Page » 1 2 3 4 5 6 7 8 9

About

Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter




Get Firefox

Monitored by Site24x7
Uptime