Regular Expressions

From EDM2
Jump to: navigation, search

The un*x to OS/2-EMX Porting FAQ

Printing from non-PM apps

Debugging

Debugging on OS/2

Regular Expressions

The world of Regular Expressions isn't as simple as it could be: there exist many versions of them: POSIX defines Basic (BRE) and Extended Regular expressions, and in addition every language and tool tries to come up with its own enhanced set (Perl, GNU utils, emacs, ...) This an incomplete, perhaps not even correct short reference list of some common denominator of Regular Expressions. Unfortunately I have no idea where I got it from.

The Metacharacters of Regular Expressions

Character Meaning
\ Turn off the special meaning of the next character
^ Indicate the beginning of the line
$ Indicate the end of the line
. Any single character
* Any number of occurances of the previous character
[ ] Any one of the characters inside the square brackets
[^ ] Any one of the characters not inside the square brackets
\( \) Tagged regular expression

The Syntax of Regular Expressions

The syntax of regular expression is basically the combination of the metacharacters and the regular characters. For example:

^My           matches line starting with the word "My"
finally$      matches ending with the word "finally"
^I am a boy$  matches the whole line of the sentence "I am a boy"
\$10          matches "$10", meaning of $ is turned off
t.e           matches "t" followed by any one character and then an "e"
my*           matches "m" and followed by any number of occurrences of "y"
th.*          matches "th" and followed by any characters including no character
[124]         matches one of "1" or "2" or "4"
[12−46]       matches one of "1" or "2" or "3" or "4" or "6". Expanded as "2−4", not twelve to forty-six.
[^abc]        matches any one except "a" and "b" and "c"