Understanding the Need
Regular expressions (or regex, for short) are tools, and like all tools, regular expressions are designed to solve a very specific problem. 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 application server) and need to display text retrieved from a database. Text may contain URLs, and you want those URLs to be clickable in the generated page (so that instead of generating just text, you generate a valid HTML ).
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).
All these scenarios present unique programming challenges. And all of them can be solved in just about any language that supports conditional processing and string manipulation. But how complex a task would the solution become? You would need to loop through words or characters one at a time, perform all sorts of if statement tests, track lots of flags so as to know what you had found and what you had not, check for whitespace and special characters, and more. And you would need to do it all manually.
Or you could use regular expressions. Each of the preceding challenges can be solved using well-crafted statements – highly concise strings containing text and special instructions – statements that may look like this:
Don’t worry if the previous line does not make sense yet; it will shortly.
my favourite book
Lesson 1. introducing regular expressions 1