

Although the term "Regular Expression" sounds tedious, and even competent UNIX® programmers often only have a vague idea of what they are, these are undoubtably the single most powerful 'feature' of UNIX. They are used (in various forms) in programs ranging from 'ls', through 'grep', 'emacs' and 'awk', to programming languages like 'python' and 'perl'. In short, if you are using UNIX seriously, you will certainly be using regular expressions.
This reference guide presents the main regular expression meta-characters. We may later extend this to include more detailed information about the different 'dialects' that various commands and languages use.
| . (dot) | Any one character |
| [...] | Any one of the characters within the square brackets |
| [^...] | Any one of the characters not within the square brackets |
| ^ | Start of line |
| $ | End of line |
| \< | Start of word |
| /> | End of word |
| | (certical bar) | Separates two expressions, matches either |
| ? | Previous character (or group) is optional |
| + | One or more of the previous character (or group) |
| * | Any number (including none) of the previous character (or group) NOTE: Matches as many as possible |
| ( ) | Three uses: 1: Used to enclose a pair of expressions, separated by | (vertical bar - see above) 2: Grouping for quantifiers ('?', '+', and '*' - see above) 3: Carry some text that matches the expression within (see '\1', etc, below) |
| \1 (and \2, \3, etc) | Output the text 'carried forward' by the brackets (see '( )' above). |
Note: When working with regular expressions, you may also find our ASCII Table useful.
What we have listed above is a very basic listing of the main 'regex' metacharacters. It is not intended to be anything more than that. It is hard to know how to expand it further without turning into a full-blown tutorial. Nearly all tutorials we have seen are seriously lacking, and we don't intend to add to their number!
Instead, we recommend the following book. What's more, we recommend this book more highly than any other book you'll find on this web site. There are two main reasons for this:
In short, if you only buy one book this year, buy this one!
Recommended book: 'Mastering Regular Expressions: Powerful Techniques for Perl and Other Tools', by Jeffrey E. F. Friedl, published by O'Reilly. You can order the book directly from here. Click here to order from Amazon.co.uk in the UK, or here to order from Amazon.com in the USA
|
(UK) |
(USA) |