Regular expressions and the regular expression language have been around for many years. Regular expression experts have long been armed with an incredibly powerful tool, one that can be used to perform all sorts of powerful text processing and manipulation in just about every language and on every platform.
What is the need??
The best way to understand regular expressions and what they do is to understand the problem they solve. Consider the following scenarios:
•You are searching for a file containing the text car (regardless of case) but do not want to also locate car in the middle of a word (for example, scar, carry,and incarcerate).
•You are generating a Web page dynamically (using an applica tion server) and need to display text retrieved from a database. Text may contain URLs, and you want those URLs to be click able in the generated page (so that instead of generating just text, you generate a valid HTML <A HREF></A>).
•You create a Web page containing a form. The form prompts for user information including an email address. You need to verify that specified addresses are formatted correctly (that they are syntactically valid).
• You are editing a source code and need to replace all occurrences of size with iSize, but only size and not size as part of another word.
• You are displaying a list of all files in your computer file system and want to filter so that you locate only files containing the text Application.
• You are importing data into an application. The data is tab delimited and your application supports CSV format files (one row per line, comma-delimited values, each possibly enclosed with quotes).
• You need to search a file for some specific text, but only at a specific location (perhaps at the start of a line or at the end of a sentence).
So What Exactly Is a Regular Expression?
Now that you know what regular expressions are used for, a definition is in order. Simply put, regular expressions are strings that are used to match and manipulate text. Regular expressions are created using the regular expression language,a specialized language designed to do everything that was just discussed and more. Like any language, regular expressions have a special syntax and instructions that you must learn.
The regular expression language is not a full programming language. It is usually not even an actual program or utility that you can install and use. More often than not, regular expressions are minilanguages built in to other languages or products.
Before You Get Started
Before you go any further, take note of a couple of important points:
•When using regular expressions,you will discover that there are almost always multiple solutions to any problem. Some may be simpler, some may be faster,some may be more portable,and
some may be more capable. There is rarely a right or wrong solution when writing regular expressions (as long as your solution works,of course).
•As already stated,differences exist between regex implementations. As much as possible,the examples used here apply to all major implementations, and differences or incompatibilities are noted as such.
Summary
Regular expressions are one of the most powerful tools available for text manipulation. The regular expressions language is used to construct regular expressions (the actual constructed string is called a regular
expression), and regular expressions are used to perform both search and replace operations.