Er. alokpandey's Blog

“Programming Quotes”

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB, WCF by Alok Kumar Pandey on February 15, 2011
  1. If debugging is the process of removing software bugs, then programming must be the process of putting them in. –  Edsger Dijkstra

  3. The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.  – Tom Cargill

  5. “There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-  C.A.R. Hoare

  7. Measuring programming progress by lines of code is like measuring aircraft building progress by weight. – Bill Gates

  9. “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” – Martin Golding

  11. “The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.” – Seymour Cray

  13. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. – Rick Cook

  15. “Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris.” – Larry Wall

  17. “Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debugging Monday’s code.” – Christopher Thompson

  19. Walking on water and developing software from a specification are easy if both are frozen. –Edward V Berard


How CRUD Is Your Design?

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB, WCF by Alok Kumar Pandey on February 9, 2011

If you’re a software developer or know one well, the acronym CRUD — Create, Read, Update and Delete — is burned into your vocabulary. These are some of the foundational elements of good software development, and each provides ways to keep your audience engaged.

When you take any one of these elements away from your users, there’s a good chance you’ll diminish the value of their experience. Here’s why.


When your audience isn’t allowed or doesn’t know how to contribute, they become passive bystanders. Your website or application no longer provides an interaction or dialogue and it probably won’t lead to a meaningful user experience.

The more you treat your audience as equals, the better. If you’re a news community like Newsvine, invite your users to create news. If you’re a information community like Wikipedia, invite your audience to create information. If you sell products like Amazon, make ratings and reviews an important part of the interface.


Confidence erodes when people can’t see data they’ve added to a website or application. They scratch their heads, wondering: Did it work? Did I do it right?

When uncertainty creeps in, people tend to blame themselves or the tool they’re using. Both scenarios are problematic, so show users where their data goes and how they can get back to it in the future.


“There are just some things you can’t take back.” This philosophy applies in the real world, but it should be avoided in a website or application.

People get nervous when they think an error they’ve made will become a permanent part of the system. So be liberal with your editing tools, and don’t hide them behind drop-downs, roll-overs or tool-tips.


People don’t like screwing up, especially in public. The ability to remove data will bring a serious dose of confidence to anyone creating content within a system. If you show them they can’t make a mistake, they’ll be much more likely to participate and give even more data.

CRUD As A Measure Of Control

There are scenarios where CRUD isn’t relevant: activity streams, automated recommendations, etc. These types of content can be interesting, exciting and even addictive, but be careful not to let them take over your website or application. Once your audience’s threshold of perceived control is lost, they’ll loose interest and move on.

So… How do you keep your audience engaged? How do you encourage them to participate? Remember, the amount of data that can be created, read, updated or deleted on your website is directly related to how in-control your audience will feel.

Leave a comment below to discuss how CRUD can make our designs more effective.


10 ways to show you’re a programming rockstar

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, Uncategorized, VB, WCF by Alok Kumar Pandey on January 27, 2011

It seems nowadays that programmers are a dime a dozen, but how do you pick the best programmers from the rest of the crowd.

It’s not just about coding (although that is a big factor). It’s about building your skill set over the years and nurturing them so you can stand out from the programming “collective.”

What characteristics makes them stand out? Are they easy to get along with? How long have they been programming? Are they teaching you things you never knew were possible? Here’s how to find out if you are a programming rock stars!

  1. Master your language and tools. Whether it’s Visual Studio, Eclipse, or even Aptana, your programming tools should be second nature to you when developing that next web application. Just like a plumber or carpenter, if you don’t have the proper tools, you won’t get the job done right.
  2. Enhance your tools and environment. With that said, even though you’ve mastered your tools, always strive to find ways to enhance your environment. This may include plug-ins to Visual Studio or Eclipse or a code generation tool that works along side your environment. If you’re not looking for better ways to enhance your productivity, you may be working yourself into an early grave.
  3. Research new technologies. While your primary language may do everything you need, make time to research your craft and experiment with new frameworks that wrap around your existing technologies. For example, while programming in web forms with C#, I heard about a new framework from Microsoft called MVC. Since I’ve started working with MVC, I’ve been programming in MVC for more than 2 years now and I haven’t looked back or regretted my decision since.
  4. Leverage your existing code you wrote. Programmers who write code and then immediately disregard it are missing out on the most exceptional tip on this list: start building your library of routines and techniques. If you are in a corporate environment, yes, you will have a corporate library to pull from for your projects. If you are an individual programmer, yes, you will have your own collection of routines or libraries that you can use in your “outside” projects. As the object-oriented saying goes, the routines are reusable.
  5. Automate like crazy. If you’ve been around the programming block for a while, you know that there are always quicker ways to accomplish certain tasks. It’s now getting to the point in this industry where if someone asks you for a web site, you can build one relatively quick. Then they start asking for features. It’s the features part that makes the difference and slows you down.
  6. Perform proper analysis. New developers always shoot first (start coding) and ask questions later. Take the time to analyze the project and ask as many questions as you can. The more questions you ask upfront, the better your ability to complete a more thorough and clear design of your application.
  7. Perform Unit Testing. Along with preceding your coding with proper analysis, always finish your coding by performing unit tests. This not only tests the quality of your code but will also let you know when your system fails on regression testing. Unit Testing should be the “checks and balances” of your programming.
  8. Extend your reach. Most developers keep to their code and that’s all they do. Break out of your comfort zone and read up on usability studies, how to document your code better, and/or using better design techniques. Expanding your skills into other areas will do three things: 1. Make you more visible to other people; 2. Make you more valuable to others because of your thirst for knowledge; and 3. Provide you with more opportunities than just programming.
  9. Communicate effectively. This is in regards to project management, coding, documentation, and impromptu meetings. If you can’t explain an extremely awesome cool coding technique to your peers or communicate why a particular feature shouldn’t be in the project to a project manager because of a time constraint, you may need to work on your verbal skills instead of your coding skills.
  10. Make time to assist others. You will seem as a definite team player by taking the time to help a fellow programmer in need. Help them understand a new concept or technique that is unclear to them and they will be grateful for the help and see you as a definite resource and go to guy.

As you can see, there are a lot of factors to take into account when deciding who is a programming rock star and how they will be perceived by other team members or by clients.

Come to think of it, you could even use some of these factors for general interview questions.

Did I miss any factors? What skills or characteristics do you think makes a programming rock star?


What if Visual Studio supported achievements

Posted in ASP.NET (C# & VB), C#, LINQ, PLINQ, SEO, SilverLight, VB by Alok Kumar Pandey on January 26, 2011

What if Visual Studio supported achievements, just like games on Steam, Xbox or PS3? Bragging to your coworkers about which one you’ve just unlocked, imagine that! Here’s a little proposed list for some of them. .NET / C# flavored, of course.

  • Falling Down – Created a new SharePoint project
  • Job Security – Written a LINQ query with over 30 lines of code
  • The Sword Fighter – 5 Consecutive Solution Rebuilds with zero code changes
  • Shotgun Debugging – 5 Consecutive Solution Rebuilds with a single character change
  • The Mathematician – Defined 15 local variables with a single character name
  • The Academic – Written 1000 lines of F#
  • Spaghetti Monster – Written a single line with more than 300 characters
  • Wild One – Mixed tabs and spaces for indentation more than 5 times in a single line
  • The Organizer – Created a Solution with more than 50 projects
  • The Portal – Created a circular project dependency
  • The Multitasker – Have more than 50 source files open at the same time
  • The Code Keeper – Uninstalled Resharper because it made you redundant
  • Pasta Chef – Created a class with more than 100 fields, properties or methods
  • Procedural Programmer – Created a method with more than 10 out parameters
  • Steam Powered – Added Visual Studio as a Steam game
  • The Poet – Written a source file with more than 10,000 lines
  • The Enterprise – Build Solution took more than 10 minutes
  • Highway to Hell – Successfully created a WCF service
  • The Explainer – Written a comment with more than 100 words
  • TPS Reports – Created a Crystal Reports Project
  • Rage Quit – ALT+F4 after a failed bug fix
  • Ooooh Shiny – Written 100 extensions methods
  • Look Ma – Written an infinite Fibonacci generator using yield
  • The Engineer – Killed a zombie with The Wrench
  • The Architect – Created 25 Interfaces in a single project
  • The Right Way – Test method is longer than the tested method
  • The Defender – Checked every argument for null exceptions
  • Pokemon Programming – Caught all the exceptions
  • Black Magic – Implemented a RealProxy
  • Gimme back my ASM – Used ILGenerator
  • I’m Sorry – Created a new Visual Basic Project
  • The SEO Expert – ASP.NET MVC Routing table with more than 100 routes
  • The Matrix – Windows Forms with more than 100 controls
  • The Daredevil – UpdatePanels nested more than 3 layers deep
  • Just a Test – Nested multiline C-style comments that caused a compilation error
  • Warm Bath – Successfully consumed a non .NET SOAP web service
  • Old School – Defined more than 100 static objects
  • The Cloner – Copy-pasted more than 50 lines
  • The Dependency – Referenced more than 30 projects
  • Paying the bills – Imported a Visual Basic project
  • First Hit – Included a library into your project and it actually compiled
  • Paula – Define a firstname field with value Brillant
  • Every Option Considered – Created an enum with more than 30 values

SQL SERVER – 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, LINQ, SEO, SilverLight by Alok Kumar Pandey on October 7, 2010

SQL SERVER – T-SQL Paging Query Technique Comparison – SQL 2000 vs SQL 2005. The main question was is this can be done using CTE? Absolutely! What about Performance? It is same! Please refer above mentioned article for history of paging.


SQL 2005 Paging Method Using Derived Table
USE AdventureWorks
SET @StartRow = 120
SET @EndRow = 140
SELECT FirstName, LastName, EmailAddress
SELECT PC.FirstName, PC.LastName, PC.EmailAddress,
ORDER BY PC.FirstName, PC.LastName,PC.ContactID) AS RowNumber
FROM Person.Contact PC) PersonContact
WHERE RowNumber > @StartRow
AND RowNumber < @EndRow
ORDER BY FirstName, LastName, EmailAddress

SQL 2005 Paging Method Using CTE
USE AdventureWorks
SET @StartRow = 120;
SET @EndRow = 140;
WITH PersonContact AS
SELECT PC.FirstName, PC.LastName, PC.EmailAddress,
ORDER BY PC.FirstName, PC.LastName,PC.ContactID) AS RowNumber
FROM Person.Contact PC)
SELECT FirstName, LastName, EmailAddress
FROM PersonContact
WHERE RowNumber > @StartRow
AND RowNumber < @EndRow
ORDER BY FirstName, LastName, EmailAddress

Following Image of Execution Plan displays that the performance for both of them is same with regarding to each other in one batch. This MAY NOT be true when there is complex query in issue. For most of the time, it is will be same.

Choosing a Project Methodology

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB by Alok Kumar Pandey on September 1, 2010

Every Project Manager has heard of one – but who actually has one? A “Methodology” that is. Whether you’re a manager, consultant, trainer, team member or student, you need some form of methodology for implementing projects. But:

  • What is a methodology?
  • How does it help you manage projects?
  • How do you select and implement one?

Choosing a Project Methodology

A Project Methodology is simply “the steps you take to deliver a project from start to finish”. It will:

  • List the phases, activities and tasks you have to complete.
  • Describe every task in detail so you know what to do.
  • Include templates so you don’t have to start from scratch.
  • Come with practical examples to guide you.
  • Give you a project roadmap ahead.

So how do I select a suitable methodology?”

1: Define your Requirements
It’s critical that you select the right methodology for your needs. So the first step is to define what those needs are by identifying the…

  • Content you need: A Methodology includes content that tells you step-by-step, how to deliver projects. You need to identify the types of content you need. For instance, should the content list every phase, activity and task in the project life cycle? Should it apply to your industry specifically, or be more generic? Should it have particular processes which are relevant to your company?
  • Features you need: The methodology should also include features that help you manage projects. For example, you may decide that it should be customizable, able to import your existing templates, able to change the terminology and add your own branding.

2: Review Your Internal Methodologies
Every project organization has its own existing internal project methodology, whether it’s simply a set of documents, templates or forms that are used for projects. So why re-invent the wheel if you have something that works in-house? Review your existing methodology, templates and forms to determine whether they meet the requirements you’ve defined above.

3: Research External Methodologies
There are a number of methodologies available for immediate adoption in the market place, such as MPMM. Go to Google and type “Project Management Methodology” and see what’s available. Compare each methodology you find to your requirements and determine the best fit. If you find a methodology that has an 80% fit, then as long as it’s customizable then you’ll be fine. Adopting and customizing a third-party methodology can save you an enormous amount of time and energy.

4: Evaluate the Creation of a Custom Methodology
Another option is to create a methodology of your own from scratch. It’s more time consuming to build a custom methodology than customize an existing one, but it may help you create unique processes for projects if you need them.

5: Select and Implement Your Methodology
Having assessed all of your available options, you now have the information you need to make your decision. Choose the methodology which is the best fit for your organization and then implement it by:

  • Adopting and customizing the methodology
  • Training your teams in the methodology
  • Publicizing your new project methodology
  • Using your new methodology for projects
  • Improving your new methodology

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB, WCF by Alok Kumar Pandey on August 30, 2010


Access to viewing and managing SageFrame portals is controlled by using User Accounts and web site Roles. The basic concept is that users can be permitted to view pages and modules, as well as edit pages and modules by being added to one or more web site roles (roles).



  • Administrators can create user accounts for any person, or they can enable site visitors to register themselves for a user account.
  • Once a user has an authorized user account, they can access all pages and modules that are restricted to members of the Registered Users role.
  • Once a user has an authorized user account, they can access all pages and modules that are restricted to members of the Registered Users role.
  • Individual users can be granted view and/or edit permission to page or module.
  • All users who are a member of a selected role can be granted view and/or edit permission to page or module.


First of all, we should be familiar with the roles that different types of editors play;

  • Module Editors: A Module Editor is any user who has been assigned permission to edit one or more modules.
  • Page Editors: A Page Editor is any user who has been assigned permission to edit one or more site pages. Page Editors can manage module content and module settings on all modules on their pages. They can also add child pages below their pages.
  • Administrators: An Administrator is any register user who is a member of the Administrator web site role. Administrators can manage all page and module content on the site.


  • Super User Accounts: A Super User Account member is any user who has been added to the Super User Accounts section of the Super User pages. These members’ permanent rights to manage all page and module content for the portal they are associated with.
  • Super User: The Super User can manage all pages and module contents on all the sites within the SageFrame Installation. Also the Super User has access to the Super User pages where portals can be created, where default settings for all portals can be managed, and portal specific Super User settings can be managed.

Overview of Role Based Access to Pages and Modules

Here we can know, how different roles are assigned for the access of Pages & Modules

  • Anonymous Users: These are users who are visiting the site but have not logged in to the site. These users may or may not be registered on the site. This is public access.
  • Registered Users: A Registered User of the site. Registered Users are able to manage their User Account online, and may also belong to one or more security roles.
  • Administrators: Administrators have the ability to edit all content and pages within a single site.

How to install SageFrame?

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB, WCF by Alok Kumar Pandey on August 17, 2010

SageFrame Installation Guide

The secret to a successful install is getting theprerequisites right first before you start the install. If everything isprepared correctly first, then installation is straightforward and painless!

Installing SageFrame is easy if you knowhow,yet there are a large number posts on the SageFrame “Install It!”forum from people who are having install problems ranging from mild tosevere.  Why is this?  The answer is that many first time installersdo not get the prerequisites right before they start to install.

Thisguide to SageFrame install prerequisites assumes that you have direct access tothe computer– for those using a hosted account, instead of installing these prerequisites,just ask your hosting provider if these prerequistes are already set up foryou, and if their control panel software will install SageFrame for you.

Youmust firstinstall these items, in this order:

1. Internet Information Server (IIS) .
2. DotNet Framework 3.5 minimum.
3. SQL Server (Express or 2005 or 2008).
4. If you are using SQL server express then you must also install SQLserver management console for SQL server express (a seperate download

There are 5 basic stepsto a clean install.

  1. Download the .zip file from
  2. Create the database and a SQL Server login
  3. Create the website folder and unzip the downloaded .zip file.
  4. Create virtual directory in Internet Information Service(IIS) and configure the website
  5. Run the SageFrame installation wizard in your favorite web browser.

1. Download file from

After you have logged into, navigate to “Download SageFrame”, selectthe desired the latest versions install package and save to your C: drive (forour example, I selected and downloaded the SageFrameSource Version ( the source code version of SageFrame allows you to make changes tothe core code of SageFrame.

2.Create the Database, SQL ServerLogin, and Database User

There are two things you need to do inSQL Server Management Studio. First, create a database.Second, create the SQL Server login for that very same database. Make surethat your database server runs in “mixed authentication mode” (SQL Server andWindows Authentication mode” is checked.) You can find this setting byright-clicking on your server (first node in the SQL ServerManagement Studio tree on the left), then Properties | Security.

Create the Database

  • Open SQL Server Management Studio
  • Select your desired server from the drop-down list and connect
  • Expand the tree on the left-hand side so you can see “Databases”
  • Right-click on “Databases”
  • Select “New Database”
  • In the “Database Name” field enter a name for your database and click OK. For this example, I entered “SF100”
  • You now have an empty database called SF100

Creating a login for your database:

  • Expand the Security node of your server
  • Right-click on Logins
  • Select “New Login”
  • Enter your desired login name. You must remember this login name and password as we will use it later. For my purposes here, I’ve entered “SF100Login” (without the quotes) as my login name.
  • Select “SQL Server Authentication” and enter a password. I entered “test” as my password.
  • Uncheck “Enforce Password expiration/User must change password at next login”
  • Click “User Mapping” on the left
  • Select/Check the new database you created in the top grid “Users mapped to this login:”
  • Select/Check db_owner in the bottom list “Database role membership for: “
  • Click OK
  • Now look in the Security node under your database (SF100) and you will see a newly created database user listed, meaning we also created a new database user by mapping our SQL Server login to our database.

The database portion is now complete.

3. Create thewebsite folder and unzip the downloaded .zip file.

In this step, we create a folder andunzip the source code version SageFrame .zip package file to this directory.You can put it directly under C:\inetpub\wwwroot or C:\foldername. Forthis example, I used C:\SF100.

  • In Windows Explorer, create a new folder called SF100 on your C: drive. This will be the folder where your website files will live.
  • Extract (unzip) the file that you downloaded earlier into your desired folder (C:\SF100 in our example)
  • Lastly, you must change the access permissions of your folder. The Windows account that is used to access your site must have full control over your SageFrame root folder. To set this, right-click on the root folder of your site (C:\SF100). Click Sharing and Security. Click the Security tab. If you do not see the Security tab, you must “turn off” simple file sharing for the folder. To do this, select the root folder (C:\SF100) in Windows Explorer. Click Tools | Folder Options. Select the View tab. Uncheck “Use simple file sharing.” Click OK. You can now right-click the folder and access the Security tab.
  • On the Security tab, you will see a list of users who have access to your folder. Windows XP uses the ASPNET account and Windows 2003 uses the NETWORK SERVICE account. Add the account and give it full control permissions.

4. IIS – createvirtual directory and set-up

Now that the database and file systemare in place, we can create the virtual directory in IIS.

  • Open IIS and expand the tree to see “Default Web Site”. (You can find IIS in Start | Control Panel | Administrative Tools.)
  • Expand the “Default Web Site” node.
  • If you have placed your SF root folder under C:\inetpub\wwwroot, you will already see your website and can bypass this step. If not, you will need to add a virtual directory.
  • Right-click on “Default Web Site”
  • Click New | Virtual Directory
  • Enter an alias – “SF100” will do
  • Click Next and enter/browse to the path to the root folder of your website.
  • Click Next and leave access permissions as is (should be set to Read/Run Scripts.)
  • Click Next and Finish

Now that a virtual directory is inplace, you’ll need to modify its properties.

  • Click on the “Documents” tab
  • An entry for “default.aspx” needs to be added. Add default.aspx and move it to the top of the default documents list.
  • Click on the “Directory Security” tab
  • In the “Anonymous access and authentication control” group box, click “Edit”
  • Make sure that “Anonymous Access” is checked as well as “Integrated Windows authentication”
  • Next, click on the “ASP.NET” tab and make sure that “2.0.50727” is select for the ASP.NET version
  • Click OK to save your changes

5. Run the SageFrameinstallation wizard in your favorite web browser to complete theinstallation process.

The very last step is to initiate the SFinstall via a web browser. The install wizard takes over. In doing so, itcreates tables and stored procedures in your database and adds the needed datarows to these tables to house your site. You will be prompted to test yourfolder’s permissions, test the database connection and lastly, enter yourdesired user name and password for your host and admin user. Write these down!

  • Open your web browser of choice and navigate to http://localhost/SF100/Install.aspx (replace SF100 with the name of your IIS virtual directory)
  • If everything has been configured properly, you will be guided through an install wizard i.e.http://localhost/SF100/Install/InstallWizard.aspx.
  • You will choose the custom install setting.  We will not choose auto option, because the custom install allows us to specify the database information.  Eventhough, the auto option will be disabled if it is your first and fresh SageFrame installation, if you are upgrading your previous version of SageFrame then the auto option will be enabled.
  • Test your folder permissions when requested.
  • Verify your database connection when asked.
  • You will see the wizard running the script for each version after you click “Next”.
  • On successful completion, click on “Finish (Access your Portal)” and your new SF-based website appear.
  • You can login as “SiteAdmin” using UserName: admin and Password: admin
  • You can login as “SuperUser” using UserName: superuser and Password: superuser

NOTE: If while installing, anything goes wrong due tomissing any of the settings above, then delete the database and create a newempty database and start the SageFrame install again.  The reason for thisis that a half completed install will result in a half created database, and itis quicker to start again than it is to fix a half installed database.Otherwise if all settings are configured correctly as mentioned above steps thenyou should check for sqldataprovider .sql scripts files under folderhttp://localhost/SF100/Install/Providers/DataProviders/SqlDataProvider/and rerun the.sql scripts by clicking “Return” button.

For Detail visit

What is SageFrame?

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, LINQ, PLINQ, SEO, SilverLight, VB by Alok Kumar Pandey on August 17, 2010

What is SageFrame?

It is an open source ASP.NET web development framework developed on the top of ASP.NET 3.5 with service pack 1 (sp1) technology, provided by Sageframe. It is a standard-based Web Content Management framework for the creation and management of dynamic web sites. It can deliver targeted content, track behavior of visitors and maintain mission critical sites with simplicity. Sageframe provides both small and large enterprises with the tools they need to be successful on the Web today.

Sageframe can be enhanced by additional products, enabling developers to quickly be productive and solve typical customer scenarios. The template projects included within Sageframe come with documented source code to jump-start projects and help the developer rapidly learn best practices during all aspects of Web application development. Sageframe reduces the ongoing costs of managing and distributing web content to large audiences, while at the same time drastically reducing one-time setup costs and overall time-to-market.

Click to this link to visit SageFrame

Fallow them on LinkedIn



Know about Regular expressions

Posted in ASP.NET (C# & VB), C#, HTML and XHTML, J-Query, JavaScript, SEO, VB by Alok Kumar Pandey on July 22, 2010

Wtf is a regular expression?

Regular expressions are strings formatted using a special pattern notation that allow you to describe and parse text. Many programmers (even some good ones) disregard regular expressions as line noise, and it’s a damned shame because they come in handy so often. Once you’ve gotten the hang of them, regular expressions can be used to solve countless real world problems.

Regular expressions work a lot like the filename “globs” in Windows or *nix that allow you to specify a number of files by using the special * or ? characters (oops, did I just use a glob while defining a glob?). But with regular expressions the special characters, or metacharacters, are far more expressive.

Like globs, regular expressions treat most characters as literal text. The regular expression mike, for example, will only match the letters m – i – k – e, in that order. But regular expressions sport an extensive set of metacharacters that make the simple glob look downright primitive.

Meet the metacharacters: ^[](){}.*?\|+$ and sometimes –

I know, they look intimidating, but they’re really nice characters once you get to know them.

The Line Anchors: ‘^’ and ‘$’

The ‘^’ (caret) and ‘$’ (dollar) metacharacters represent the start and end of a line of text, respectively. As I mentioned earlier, the regular expression mike will match the letters m – i – k – e, but it will match anywhere in a line (e.g. it would match “I’m mike”, or even “carmike”). The ‘^’ character is used to anchor the match to the start of the line, so ^mike would only find lines that start with mike. Similarly, the expressionmike$ would only find m – i – k – e at the end of a line (but would still match ‘carmike’).

If we combine the two line anchor characters we can search for lines of text that contain a specific sequence of characters. The expression ^mike$ will only match the word mike on a line by itself – nothing more, nothing less. Similarly the expression ^$ is useful for finding empty lines, where the beginning of the line is promptly followed by the end.

The Character Class: ‘[]‘

Square brackets, called a character class, let you match any one of several characters. Suppose you want to match the word ‘gray’, but also want to find it if it was spelled ‘grey’. A character class will allow you to match either. The regular expressiongr[ea]y is interpreted as “g, followed by r, followed by either an e or an a, followed by y.”

If you use [^ ... ] instead of [ ... ], the class matches any character that isn’tlisted. The leading ^ “negates” the list. Instead of listing all of the characters you want to included in the class, you list the characters you don’t want included. Note that the ^ (caret) character used here has a different meaning when it’s used outside of a character class, where it is used to match the beginning of a line.

The Character Class Metacharacter: ‘-’

Within a character-class, the character-class metacharacter ‘-’ (dash) indicates a range of characters. Instead of [01234567890abcdefABCDEF] we can write [0-9a-fA-F]. How convenient. The dash is a metacharacter only within a character class, elsewhere it simply matches the normal dash character.

But wait, there’s a catch. If a dash is the first character in a character class it is not considered a metacharacter (it can’t possibly represent a range, since a range requires a beginning and an end), and will match a normal dash character. Similarly, the question mark and period are usually regex metacharacters, but not when they’re inside a class (in the class [-0-9.?] the only special character is the dash between the 0 and 9).

Matching Any Character With a Dot: ‘.’

The ‘.’ metacharacter (called a dot or point) is shorthand for a character class that matches any character. It’s very convenient when you want to match any character at a particular position in a string. Once again, the dot metacharacter is not a metacharacter when it’s inside of a character class. Are you beginning to see a pattern? The list of metacharacters is different inside and outside of a character class.

The Alternation Metacharacter: ‘|’

The ‘|’ metacharacter, (pipe) means “or.” It allows you to combine multiple expressions into a single expression that matches any of the individual ones. The subexpressions are called alternatives.

For example, Mike and Michael are separate expressions, but Mike|Michael is one expression that matches either.

Parenthesis can be used to limit the scope of the alternatives. I could shorten our previous expression that matched Mike or Michael with creative use of parenthesis. The expression Mi(ke|chael) matches the same thing. I probably would use the first expression in practice, however, as it is more legible and therefore more maintainable.

Matching Optional Items: ‘?’

The ‘?’ metacharacter (question mark) means optional. It is placed after a character that is allowed, but not required, at a certain point in an expression. The question mark attaches only to the immediately preceding character.

If I wanted to match the english or american versions of the word ‘flavor’ I could use the regex flavou?r, which is interpreted as “f, followed by l, followed by a, followed by v, followed by o, followed by an optional u, followed by r.”

The Other Quantifiers: ‘+’ and ‘*’

Like the question mark, the ‘+’ (plus) and ‘*’ (star) metacharacters affect the number of times the preceding character can appear in the expression (with ‘?’ the preceding character could appear 0 or 1 times). The metacharacter ‘+’ matches one or more of the immediately preceding item, while ‘*’ matches any number of the preceding item, including 0.

If I was trying to determine the score of a soccer match by counting the number of times the announcer said the word ‘goal’ in a transcript, I might use the regular expression go+al, which would match ‘goal’, as well as ‘gooooooooooooooooal’ (but not ‘gal’).

The three metacharacters, question mark, plus, and star are called quantifiers because they influence the quantity of the item they’re attached to.

The Interval Quantifier: ‘{}’

The ‘{min, max}’ metasequence allows you to specify the number of times a particular item can match by providing your own minimum and maximum. The regex go{1,5}alwould limit our previous example to matching between one and five o’s. The sequence {0,1} is identical to a question mark.

The Escape Character: ‘\’

The ‘\’ metacharacter (backslash) is used to escape metacharacters that have special meaning so you can match them in patterns. For example, if you would like to match the ‘?’ or ‘\’ characters, you can precede them with a backslash, which removes their meaning: ‘\?’ or ‘\\’.

When used before a non-metacharacter a backslash can have a different meaning depending on the flavor of regular expression you’re using. For perl compatible regular expressions (PCREs) you can check out the perldoc page for perl regular expressions. PCREs are extremely common, this flavor of regexes can be used in PHPRuby, andECMAScript/Javascript, and many other languages.

Using Parenthesis for Matching: ‘()’

Most regular expression tools will allow you to capture a particular subset of an expression with parenthesis. I could match the domain portion of a URL by using an expression like http://(%5B^/]+). Let’s break this expression down into it’s components to see how it works.

The beginning of the expression is fairly straightforward: it matches the sequence “h – t – t – p – : – / – /”. This initial sequence is followed by parenthesis, which are used tocapture the characters that match the subexpression they surround. In this case the subexpression is ‘[^/]+’, which matches any character except for ‘/’ one or more times. For a URL like, ‘’ will be captured by the parenthesis.