Article View: demon.ip.support.mac
Article #5698RegExps Key
From: Pi
Date: Sun, 05 Nov 1995 00:00
Date: Sun, 05 Nov 1995 00:00
81 lines
3806 bytes
3806 bytes
Remember I said there was a great text accompanying the PC tool 'Trex'?
Here are the most relevant bits of that file. You have to make allowances
for the fact is supposed to be run on a command line, but it explains quite
nicely how regular expressions work. Mostly Mac users will be interested
in the part of the commands in quotation marks. There is more to the doc,
but it mostly concerns practicing these manouvers.
----------------------------------------8<----------------------------------
Normal usage: trex ".*phrase.*" <filename.doc
'.' : any character.
'*' : zero or more occurances
'+' : one or more occurances
'?' : zero or one occurance
'|' : 'or', separates alternatives
'[' and ']' : used to define ranges
'(' and ')' : used for grouping
'^' : beginning of line
'$' : end of line
'\' : next character 'as-is'
trex "^ +.trex.*" <trex.doc
This matches (and prints) every line that starts with at least one space,
then has the string 'trex'. It filters out the trex examples we saw up to
now. Let's analyze how it works. The '^' ensures that the beginning of the
line is matched. The '+' following the space character, indicates that at
least one space (possibly more) has to be present. The '.*' behind 'trex'
is not required to _find_ the line, but to output it entirely. (You know
that trick by now) Can you find out what Trex command would print out all
lines ending in ':' in this file ? Here's the solution:
trex ".*:$" <trex.doc
Lets have a look at ranges now. Suppose you want to find numerical values
in a file. You could use this:
trex "[0123456789]+" <trex.doc
The '[' and ']' simply enclose a list of valid characters. (Digits, in this
case). The trailing '+' indicates that Trex should try to match as many of
them as possible, but at least one. There's an easier way, to do this. You
can use a dash in a range to specify 'all characters in between'. Like
this:
trex "[0-9]+" <trex.doc
OK, now suppose we'd like to locate values with a preceding dollars sign,
like: $1000 or $99.95 etc. Two problems. First, we can't use the '$' or
'.' sign - both are special characters, and in this case we don't want them
to have special action. The '\' sign is the solution - it instructs Trex to
ignore the special meaning - if any - of the following character. So to
match the $1000 line above, you can use:
trex "\$[0-9]+" <trex.doc
Ok, so far so good, but what about the $99.95 ? Only the '$99' part got
output - not what we wanted! We could change our line to: trex
"\$[0-9]+\.[0-9]+" <trex.doc but now, _only_ '$99.95' will be matched. Not
what we want either. Here's a possible solution:
trex "\$[0-9]+(\.[0-9]+)?" <trex.doc
This is the first time that we see both the grouping parentheses and the
'?' character. The question mark says 'zero or one occurences of the
previous patterns', and the parentheses take care that the 'previous
pattern' is what we want it to be. We didn't use the '|' character yet. It
is used to separate alternatives. To search for HP95 or HP100, for example,
you could use: HP95|HP100 or HP(95|100).
_______________PGP Spoken Here_______________
Pluralitas non ponenda est sine necessitate
http://cent1.lancs.ac.uk/~phb007
pi@macabre.demon.co.uk
Technical Details
Message-ID:
<ACC1DC989668126BED@macabre.demon.co.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f989,230d18d4a1dd259a,start X-Google-Attributes: gid10f989,public From: pi@macabre.demon.co.uk (Pi) Subject: RegExps Key Date: 1995/11/05 Message-ID: <ACC1DC989668126BED@macabre.demon.co.uk> X-Deja-AN: 118694456 x-nntp-posting-host: macabre.demon.co.uk organization: Macabre Merchandising newsgroups: demon.ip.support.mac