QuickReferenceToRegularExpressions

From brainsik
Jump to navigation Jump to search

==Quick Reference to Regular Expressions ==

 ^   means beginning of line
 $   means end of line
 .   means match anything
 +   means match the previous thing 1 or more times
 *   means match the previous thing 0 or more times
 [[[]]  means matching any character inside of [[[]]

====Examples:

==

 .+    match anything one or more times
       matches 'cat', 'no dogs', 'dk jf   3h 8yudkj-_+$^%'
 .*    same as above, but also matches 
 [[[Aa]]  matches 'a' or 'A'

Regular expressions are typically greedy, so matches will try to match as much as possible, thus, if you have the string

 "the one the two the three"

and you use the regular expression

 .+the

then

 .+   matches 'the one the two '
 the  mathces 'the'

so

 .+the  matches 'the one the two the'


So, these are good to know because they are used everywhere. For example, if you are reading a file with 'less' and you hit '/' to do a search. You are using regular expressions so all that syntax above is valid.

If you are using 'grep' to search a file, this syntax is valid. For example:

 grep '^To: brainsik-sucks@theory.org' mailbox

I use single quotes so that the shell doesn't try to interpret any of the regular expression characters as shell characters.

Emacs, Vim, almost anywhere there is a search feature, you can use regular expressions. There are different kinds of regular expression syntax, but these are universal.

Also, you will often see regular expressions contained within '/', like:

  /.+the/

For exmaple, in your .mailfilter file.


Back to LinuxNotes


Last Edit: Tue, 17 Sep 2002 11:32:57 -0700
Revisions: 3