[an error occurred while processing this directive]
Regular Expression
Regular Expressions are the standard UNIX pattern matching language. Regular expressions are used heavily for searching and parsing text in most all UNIX software.
Regular expressions allow you to construct a template or pattern for what you're searching for. In the UNIX environment, you can then use your pattern template to determine whether a given text fragment matches your pattern.
For example, suppose you wanted to find all lines in a file starting with the letter N. You'd want to construct a regular expression which stood for "has a letter N as the first character on the line". The regular expression for this is: /^N/. The two slashes mark the beginning and the end of the regular expression, so the regular expression itself is just the ^N. The '^' in regexp syntax means "beginning of line". So now you should be able to see how /^N/ means "line beginning with the letter N. You would then use a UNIX tool that knew how to take a regular expression and a file name and print all lines in that file matching that regexp (the UNIX 'grep' utility would typically be used to do this).
Now let's say we wanted to find lines whose first word started with N, and whose second word started with the letter 'g'. Let's say we also don't care whether the letters are upper or lower case. Here's the regular expression meaning "all lines whose first word begins with n and whose second word begins with g": /^n[A-Za-z0-9]+\s+g/.
Taking this apart: the two forward slashes mark the beginning and end of the regexp, so the regexp itself is ^n.+\s+g.+. The 'i' after the closing slash makes the pattern case insensitive, so both upper and lower case N's and G's will match.
The other three main components of the pattern are 'n' '\s' and 'g'. The n means the letter n, the g the letter g, and the '\s' is a special character meaning "whitespace", or "any spaces", which could be a space, a tab, or a line break. Notice that both the n and the g are followed by a period. The period in regexp syntax means "any character". Therefore, n. or g. means n followed any character and g followed by any character, respectively.
Finally, notice the 3 plus signs. A plus sign (+) in regular expressions means "one or more" and it always applies to the character immediately preceding it. Therefore, a+ means "one or more a's", "\s+" means "one or more whitespaces", etc. Since . means "any character", then, .+ means "one or more of any character", and "n.+" means "the letter n followed by one or more letters.
Therefore, ^n.+\s+g.+ means "an n at the beginning of the line followed by one or more characters followed by one or more spaces, tabs, etc., followed by a g, followed by one or more characters.
Suppose we wanted to match all lines whose first word started with n and whose second word started with g, but which may or may not be indented by spaces before the first word. In other words, we still want the above regexp to match even if the first word is preceded by one or more spaces. Since we also want to match if the first word is not preceded by any spaces at all, the most succinct way to put it is 'match even if first word is preceded by zero or more spaces'.
We saw that the plus sign stands for 'one or more'. Similarly, the asterisk (*) stands for 'zero or more', and, like the plus, applies to the immediately preceding character.
So our new regexp for finding all lines whose first word begins with n and whose second word begins with a g (upper or lower case), and which may or may not be indented with whitespaces, is: /^\s*n.+\s+g.+/i. All we've done is preceded the first n with '\s*' which means 'zero or more whitespaces'.
For a more thorough tutorial on regular expressions, a good source is the O'Reilly & Associates nutshell guide to either Sed & Awk or Perl. These are UNIX programming languages which are based heavily on regexp pattern matching, and the O'Reilly guide for each contains a chapter devoted to learning regular expressions.
For your reference, a list of regexp special characters and their meanings follows.