Regular Expressions A. K. A. Regex

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.

Loading

3 Comments

  1. Sudath Hagoda arachchi August 9, 2012 at 4:17 am

    This site is good presentation. It is structured as programmer’s vision. You should offer your website readers programming solution, Algorithms and new programming ideas but it must be with human touch.
    We all are human beings therefore every software solution must end with human face.

    Everybody attracts love, happiness, appreciation, closeness, friendliness, sensitive feelings, kindness, affection, mercy, pleasure, and tender.

    If you can present with artistic materials such as ideas and vision about, what is happening in the society around you. Programmers will enjoy them as additional fun. Most of programmers or software engineers publish their websites with heavily structured version of programming, Algorithms and latest software solutions. It will be rather monotonous to website visitors.

    GIVE HUMAN FACE FOR YOUR WEBSITE WITH TENDER, LOVING AND CARE

    World greatest thing is humanity. The World without humanity does not exist.

    I wish you to be greatest creative software engineer and most effective, powerful administrator in the world.

    Good Luck,

    Reply
  2. admin August 13, 2012 at 11:55 am

    Thanks for the comment :-)

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.