The Basics of Regular Expressions
Regex is, essentially, a whole new language, with its rules, its structures, and its quirks. You'll also find that your knowledge of most other programming languages will have practically no bearing on learning regex, for the simple reason that regular expressions are highly specialized and follow their own rules.
As defined by Kleene, the basic regex axioms are the following:
This may sound complicated to you, and I'm pretty positive that it scared me the first time I read through it. However, the basics are easy to understand. First, the simplest regular expression is a single character. For example, the regex a will match the character "a" of the word Marco. Notice that, under normal circumstances, regex are binary operations, so that "a" is not equivalent to "A". Therefore, the regex a will not match the "A" in MARCO.
So far, regular expressions are not very different from normal search operations. However, this is where the similarities end. As I mentioned earlier, you can use Kleene's Star to create a regular expression that can be repeated any number of times (including none). For example, consider the following string:
seeking the treasures of the sea
The regex se* will be interpreted as "the letter s followed by zero or more instances of the letter e" and match the following:
It's important to understand that, in the preceding expression, only the expression e is considered when dealing with the star. Although it's possible to use parentheses to group regular expressions, you should not be tempted to think that using (se)* is a good idea, because the regex compiler will interpret it as meaning "zero or more occurrences" of "se."
grab the grub and pull