Latest Posts »
Latest Comments »
Popular Posts »

Regex

Written by meena on August 5, 2005 – 1:36 pm

Whitespace = /^\s+$/;
Letter = /^[a-zA-Z]$/;
Alphabetic = /^[a-zA-Z]+$/;
Alphnumeric = /^[a-zA-Z0-9]+$/;
Digit = /^\d/;
LetterOrDigit = /^[a-zA-Z]\d$/;
Integer = /^\d+/;
SignedInteger = /^(\+-)?\d+$/;
ZipCode = /^\d{5}$^\d{5}\-\d{4}$/;
Email = /^([a-z\.]+)@([a-z\.]+[\.][a-z]{2,3})$/;
onClick=”validate(form.email.value, ‘^([a-z\.]+)@([a-z\.]+[\.][a-z]{2,3})$’)”>

Let’s look at that regular expression closely.
[a-z\.] means match any lowercase letter or a period.
The + says to match that pattern one or more times.
The ^ matches the beginning of the line or string and the $ signifies the end of the line.
If there were extra digits at the beginning or end of the first sample pattern, it would still find the required pattern string, but the extra digits would get ignored.
The surrounding ^ and $ force a match on the entire string.

Some More..

Regular expression syntax is as follows. Some programs will accept all of these, others may only accept some.
. match any single character except
* match zero or more instances of the single character (or meta-character) immediately preceding it
[abc] match any of the characters enclosed
[a-d] match any character in the enclosed range
[^exp] match any character not in the following expression
^abc the regular expression must start at the beginning of the line (Anchor)
abc$ the regular expression must end at the end of the line (Anchor)
separator. Match either the preceding or following expression.
( ) group the regular expressions within and apply the match to the set.
Some examples of the more commonly used regular expressions are:
cat the string cat
.at any occurrence of a letter, followed by at, such as cat, rat, mat, bat, fat, hat
xy*z any occurrence of an x, followed by zero or more y’s, followed by a z.
^cat cat at the beginning of the line
cat$ cat at the end of the line
\* any occurrence of an asterisk
[cC]at cat or Cat
[^a-zA-Z] any occurrence of a non-alphabetic character
[0-9]$ any line ending with a number
[A-Z][A-Z]* one or more upper case letters
[A-Z]* zero or more upper case letters (In other words, anything.)


Posted in Functional Testing |

Leave a Comment