Wednesday, September 26, 2007

Regular Expressions

As developers, we all use regular expressions at one time or other. But the problem is we do not have to use them often enough to remember them until we have to use them next time. Recently I had to use regex in validation.xml after a long gap. This information is mostly from different blogs, Google and other resources. This blog entry is for me as a future reference when I have to use them again. You can use it if you find useful.


  1. * : zero or more times in any combinations

Ex : [0-8]*

Input: ‘5’, ‘’, ‘2234’

Above expression states that allowed digits are 0 to 8 and can occur any number of times.

  1. [^0-8] : negation, not even once

Above expression states those digits 0 to 8 cannot occur. So only 9 is valid.

  1. z : ‘z’ must occur only once.
  2. z+ : ‘z’ must occur once or more. The difference between z+ and z* is in later case ‘z’ can occur zero times but in former case it must occur at least once.
  3. z{2} : ‘z’ must occur exactly 2 times.
  4. z{2,4} : ‘z’ must occur at least 2 times but not more than 4 times.
  5. z {2, } : ‘z’ must occur at least times and any number of times.
8 . z? : ‘z’ is optional but if it is there it cannot occur more than one time.


No comments: