Monday, June 18, 2007

Regular Expressions

Regular Expression:

^ : Matches the Beginning of string
$ : Matches the End of string
(): Parentheses, define the scope and precedence of Operators

Quantification: How often the preceding expression is allowed
There are 3: ?, * and +

?: 0 or 1 [ colou?r will match color or colour ]
*: 0, 1 or any number [ go*gol will match ggol, gogol, googol, gooogol etc ]
+: at least 1 [ go+gol = gogol, googol, gooogol etc ]

{n}: Repeat n times the previous exp

Examples:

1xx : ^1[0-9]{x}
21xx or 22xx: ^2[12][0-9]{2}
3xxxxxxxxxx, 4xxxxxxxxx, 9xxxxxxxxx (10 digits telephone number starting with 3, 4 or 9)
^[349][0-9]{9}

No comments:

Whats new in Ajuby 0.5