home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
lgrep_v2.lzh
/
regex
< prev
Wrap
Text File
|
1994-01-17
|
3KB
|
91 lines
Regular expressions are studied by every Computer Science Master's
student, so...
A little bit of Computer Science.....
A regular expression is a method or notation of describing a regular
language. A language is called "regular" if there exists a finite
acceptor for it. That is to say, a finite state machine exists that
describes the language. [1]
.... ok, that was fun, but what does it mean to grep.....
A regular expression is the "machine" that is "executed" against a string.
The string will either "execute" TRUE or "execute" FALSE. There is a
specific notation to a regular expression. Traditionally, grep uses the
following:
^ A circumflex at the beginning of an expression matches the
beginning of a line.
$ A dollar-sign at the end of an expression matches the end of a line.
. A period matches any character except the newline.
* An expression followed by an asterisk matches zero or more
occurrances of that expression.
+ An expression followed by a plus sign matches one or more
occurrances of that expression.
[] A string enclosed in square brackets matches any character in
that string, but no others. If the first character in the
string is a circumflex, the expression matches any character
except newline and the characters in the string. A range of
characters may be specified by two characters separated by "-"
: A colon is always followed by another character which matches an
indicated class. ":a" mactches any alphabetic character. ":d"
matches any digit character. ":n" matches any alphanumeric
character. ": " matches spaces, tabs and other control characters,
such as newline.
\ The backslash quotes any character.
x An ordinary character which is not mentioned above matches that
character.
The concatenation of a regular expression is another regular expression.
So... now for some examples:
Regular expression String Result
abc abcdef TRUE
abc$ abcdef FALSE
..abc.. ababcab TRUE
xyabcay TRUE
.*def abcdef TRUE
:dfoo 1foo TRUE
:d+foo 123foo TRUE
afoo FALSE
:d*foo 123foo TRUE
afoo TRUE
[abc] axyz TRUE
uxyz FALSE
[^abc] abc FALSE
abc\.xyz abc.xyz TRUE
abcYxyz FALSE
[1] An Introduction to Formal Languages and Automata, Peter Linz,
D.C. Heath and Co., ISBN: 0-669-17342-8
Brad Spencer
brad@anduin.eldar.org
spencer@austin.onu.edu