Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



REGULAR EXPRESSIONS


Regular expressions are more or less the same as the Global ones, but are mostly used to search strings ( any sequence of characters ) and to perform changes, within a file. ( We use Global expressions to search files, Regular expressions to search in files )
They are widely used with the following commands: ¨grep¨, ¨egrep¨ and ¨sed¨. Also for searches in the ¨vi¨ editor we use Regular expressions.
We will keep our search limited to ¨grep¨.

NOTE: First 2 striking differences with the Global expressions:
¨?¨ stands for 1 random character in Global expressions, ¨.¨ is the sign for it in Regular expressions.
[! . . . ] stands for ¨not¨ in Global expressions, and [^ . . . ] is what we use in Regular expressions.

In fact, the ¨^¨ has 2 meanings in a regular expression, depending on its place: ¨[^¨ means ¨not¨ , where just ¨^¨ at the beginning of the expression means that the string or word should be at the beginning of the line.
A ¨$¨ at the end of the expression means that the word should be at the end of the line. ( This will all become clear in the examples. )
One more thing: the regular expression always starts and ends with ' ( an apostrophe ).

Grep will search a string of characters specified by the Regular expression and print the lines in which it finds those strings to the screen.


O.K. Here are some simple examples:

CODE
$ grep -n 'cr[i-z]ft' example-file

Will print the lines with ¨cruft¨ and ¨croft¨ in it ( with the line numbers )


CODE
$ grep -n 'sh[^a-h]re' example-file

Prints the lines with ¨shore¨ and ¨shire¨


CODE
$ grep -n  '^ Fast. *them $' example-file

Will search for the lines that starts with the word ¨Fast¨ and end with the word ¨them¨


Regular expressions interpret the . \ [ ] * in a special way, so if you want them to be part of the string you look for, you need to use the escape sign: \

Another formula: \{ . . . \} tells grep how many times a character should show: a\{2\} means 2x the ¨a¨

Example: Imagine you have a long list of names and telephone numbers, and you want to search for a name that starts with a ¨B¨ and has 66 in the phone number.

CODE
$ grep -n '^ B* 6\{2\} * $' phonelist


Another little hint:

CODE
$ grep -n 'bat' example-file

Will find lines with the words ¨bathroom¨, ¨batter¨ and so on.


CODE
$ grep -n '\<bat\>' example-file

Only finds the line with the word ¨bat¨ in it



O.K. We had enough complicated stuff let's have some FUN , try this one:

CODE
$ grep -e '^\(.\)\(.\)$' /usr/share/dict/words

To find 4 letter palindromes

Or:

CODE
$ grep -e '^\(.\)\(.\)\(.\)$' /usr/share/dict/words

To find 6 letter ones



So this gave you a little taste of what a Regular expression is. For a deeper insight see: Here.
Or if you really want to know it all, the ¨Mastering Regular Expressions¨ book at O'Reilly or at your local bookstore.


Bruno


-- Jun 24 2003 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email