Index
[]
Index
[]
Index
[]
Index
[]devicedirectorydisk
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]Internet
Index
[]
Index
[]
Index
[]library
Index
[]
Index
[]network
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
Index
[]
[Page 595]
Appendix A.
[Page 595 (continued)]
A.1. Regular Expressions
Regular expressions are character sequences that describe a family of matching strings. They are accepted as arguments to many GNU utilities, such as grep, egrep, gawk, sed, and vim . Note that the filename substitution wildcards used by the shells are not examples of regular expressions, as they use different matching rules.
Regular expressions are formed out of a sequence of normal character and special characters. is a list of special characters, sometimes called metacharacters , together with their meaning.
Figure A-1. Regular expression metacharacters.
Metacharacter | Meaning |
---|
. | Matches any single character. |
[ ] | Matches any of the single characters enclosed in brackets. A hyphen may be used to represent a range of characters. If the first character after the [ is a ^, then any character not enclosed in brackets is matched. The *, ^, $, and \ metacharacters lose their normal special meaning when used inside brackets. |
* | May follow any character, and denotes zero or more occurrences of the character that precedes it. |
^ | Matches the beginning of a line only. |
$ | Matches the end of a line only. |
\ | The meaning of any metacharacter may be inhibited by preceding it with a \. |
[Page 596]
A regular expression matches the longest pattern that it can. For example, when the pattern "y.*ba" is searched for in the string "yabadabadoo", the match occurs against the substring "yabadaba" and not "yaba". The next page contains some examples of regular expressions in action.
To illustrate the use of these metacharacters, here is a piece of text followed by the lines of text that would match various regular expressions. The portion of each line that satisfies the regular expression is italicized.
A.1.1. Text
Well you know it's your bedtime,So turn off the light,Say all your prayers and then,Oh you sleepy young heads dream of wonderful things,Beautiful mermaids will swim through the sea,And you will be swimming there too.
A.1.2. Patterns
lists lines that match regular expression patterns.
Figure A-2. Lines matching regular expression patterns.
Pattern | Lines that match |
---|
the | So turn off the light, Say all your prayers and the n, Beautiful mermaids will swim through the sea, And you will be swimming the re too. |
.nd | Say all your prayers and then, Oh you sleepy young heads dream of w ond erful things, And you will be swimming there too. |
^.nd | And you will be swimming there too. |
sw.*ng | And you will be swimming there too. |
[A-D] | B eautiful mermaids will swim through the sea, A nd you will be swimming there too. |
\. | And you will be swimming there too . (the "." matches) |
a. | S ay all your prayers and then, Oh you sleepy young he ad s dream of wonderful things, Be au tiful mermaids will swim through the sea, |
a.$ | Beautiful mermaids will swim through the se a , |
[a-m]nd | Say all your prayers and then, |
[^a-m]nd | Oh you sleepy young heads dream of w ond erful things, And you will be swimming there too. |
[Page 597]
A.2. Extended Regular Expressions
.
Figure A-3. Extended regular expression metacharacters.
Metacharacter | Meaning |
---|
+ | Matches one or more occurrences of the single preceding character. |
? | Matches zero or one occurrence of the single preceding character. |
| (pipe symbol) | If you place a pipe symbol between two regular expressions, a string that matches either expression will be accepted. In other words, a | acts like an "or" operator. |
() | If you place a regular expression in parentheses, you may use the *, +, or ? metacharacters to operate on the entire expression, rather than just a single character. |
.
Figure A-4. Lines matching extended regular expression patterns.
Pattern | Lines that match |
---|
s.*w | Oh you sleepy young heads dream of w onderful things, Beautiful mermaid s will sw im through the sea, And you will be sw imming there too. |
s.+w | Oh you sleepy young heads dream of w onderful things, Beautiful mermaid s will sw im through the sea, |
off|will | So turn off the light, Beautiful mermaids will swim through the sea, And you will be swimming there too. |
im*ing | And you will be sw imming there too. |
im?ing |
[Page 597 (continued)]
A.3. Modified Backus-Naur Notation
The syntax of the GNU utilities and Linux system calls in this book are presented in a modified version of a language known as Backus-Naur Form, or BNF for short. In a BNF description, the sequences in have a special meaning.
[Page 598]
Figure A-5. BNF notations used in this book.
Sequence | Meaning |
---|
[ strings ] | Strings may appear zero or one time. |
{ strings }* | Strings may appear zero or more times. |
{ strings }+ | Strings may appear one or more times. |
string1|string2 | string1 or string2 may appear. |
-optionlist | Zero or more options may follow a dash. |
The last sequence is the Linux/GNU-oriented modification, which allows me to avoid placing large numbers of brackets around command-line options. To indicate a [, {, |, or - without its special meaning, I precede it with a \ character.
Some variations of commands depend on which option you choose. I indicate this by supplying a separate syntax description for each variation. For example, take a look at the syntax description of the at utility ().
Figure A-6. Example description of the at command
Utility : at -csm time [ date [, year ]][ + increment ][ script ] |