Let’s take a look at what’s regular expression and it’s use in the development environment. 1st of all regular expression is a small piece of code which looks for pattern(s). In web development environment Regex are used in the .htaccess file most frequently. Regexs are supposed to make developer’s life easier but most of the time it’s the other way around. Let’s stop the chit-chat and go through some technical aspects of Regexs.

[ ] – a portion of the expression.

letter-letter – matches any single lowercase alphabetical character.
Eg :- [a-z] – looks for lower case letters that are in the range of a to z.

LETTER-LETTER – matches any single capital alphabetical character.
Eg :- [A-Z] – looks for upper case letters that are in the range of A to Z.

number-number – matches any single number.
Eg :- [4-6] – would match any single number 4, 5, or 6.

character list – matches any single character.
Eg :- [abc123] matches any single character, either a, b, c, 1, 2, or 3. Keep in mind that in this example Regex won’t look for word abc123. It’s just individual letter.

^ has two purposes

  • When used inside of [] it designates ‘NOT’. [^abc] would match any character that is not a lowercase a, b, or c.
  • When used at the beginning of a pattern in mod_rewrite, it also designates the beginning of a ‘line’.

. (a dot) matches any single character, except the ending of a line.

?  matches 0 or 1 of the characters.
Eg :- a? would match letter ‘a’ zero or one time. As well as (abc)? would match the phrase ‘abc’ 0 or 1 time.
+ matches 1 or more of the characters.

Eg :- a+ would match letter ‘a’ one or more than one time. As well as (abc)+ would match the phrase ‘abc’ 1 or more than 1 time.

* matches 0 or more of the characters
Eg :- a* would match letter ‘a’ zero or more times. As well as (abc)* would match the phrase ‘abc’ zero or more times.

( ) creates a variable to be stored and possibly used later, and is also used to group text for use with the quantifiers ?, +, and * described above.

$ defines the ending of a ‘line’.

In the next article we’ll talk about the usage of Regexs in the .htaccess and some more stuff which can be useful.

As always your feedback is welcome. Give your ideas about the posts.

Be Sociable, Share!