Teil 07: Anker, Praezedenz, Zusammenfassung
Kommen wir nun zu den letzten zwei Metazeichen von EREs: Anker. Mit ihnen kann man einen Ausdruck am Zeilenanfang bzw. Zeilenende verankern. Anker sind reine Metainformation, sie matchen auf keine Zeichen, sondern sagen nur etwas darueber aus, wo auf der Zeile der Ausdruck gematcht werden kann.
Es gibt zwei Anker in EREs:
Der Zeilenanfangsanker wir mit dem Circumflex (^) notiert. Die RE:
Code: Alles auswählen
^foo
Der Zeilenendeanker wird mit dem Dollarzeichen ($) notiert. Die RE:
Code: Alles auswählen
bar$
Im Falle von EREs ist es erlaubt, diese Anker auch in die Mitte einer RE einzubauen, auch wenn sie dort niemals matchen koennen. (Das ist bei anderen RE-Varianten anders.)
Damit haben wir nun alle Funktionen von POSIX EREs kennengelernt, lediglich Collation Symbols ([. .]) und Equivalence Class Expressions ([= =]) innerhalb von Zeichenklassen haben wir ausgelassen. Diese habe ich bislang auch noch nie in der Praxis gesehen.
POSIX-Beschreibung von EREs:
https://pubs.opengroup.org/onlinepubs/9 ... #tag_09_04
Praezedenz (von hoch zu niedrig)
1. Collation-related bracket symbols innerhalb von Zeichenklassen
[==] [::] [..]
2. Escapete Metazeichen
\
3. Zeichenklassen
[]
4. Unterausdruecke
()
5. Quantoren
* + ? {m,n}
6. Verkettung
(ohne Operator)
7. Anker
^ $
8. Alternation
|
Mit Unterausdruecken (runden Klammern) kann man die Praezedenz anpassen.
Zum Schluss moechte ich nochmal Henry Spencers RE-Beschreibung aus der Manpage regex(7) hervorholen. Anmerkungen blau in eckingen Klammern.
Manpage regex(7) hat geschrieben: Regular expressions ("RE"s), as defined in POSIX.2, come
in two forms: modern REs (roughly those of egrep; POSIX.2
calls these "extended" REs) and [...BREs...].
POSIX.2 leaves
some aspects of RE syntax and semantics open; "(!)" marks
decisions on these aspects that may not be fully portable
to other POSIX.2 implementations.
A (modern) RE is one(!) or more nonempty(!) branches,
separated by '|'. It matches anything that matches one
of the branches.
[= Alternation]
A branch is one(!) or more pieces, concatenated. It
matches a match for the first, followed by a match for
the second, etc.
[= Verkettung]
A piece is an atom possibly followed by a single(!) '*',
'+', '?', or bound. An atom followed by '*' matches a
sequence of 0 or more matches of the atom. An atom fol‐
lowed by '+' matches a sequence of 1 or more matches of
the atom. An atom followed by '?' matches a sequence of
0 or 1 matches of the atom.
[= Quantoren]
A bound is '{' followed by an unsigned decimal integer,
possibly followed by ',' possibly followed by another
unsigned decimal integer, always followed by '}'. The
integers must lie between 0 and RE_DUP_MAX (255(!))
inclusive, and if there are two of them, the first may
not exceed the second. An atom followed by a bound con‐
taining one integer i and no comma matches a sequence of
exactly i matches of the atom. An atom followed by a
bound containing one integer i and a comma matches a
sequence of i or more matches of the atom. An atom fol‐
lowed by a bound containing two integers i and j matches
a sequence of i through j (inclusive) matches of the
atom.
[= Quantoren]
An atom is a regular expression enclosed in "()" (match‐
ing a match for the regular expression), an empty set of
"()" (matching the null string)(!),
[= Unterausdruck]
a bracket expression (see below),
[= Zeichenklasse]
'.' (matching any single character),
[= Punkt]
'^' (matching the null string at the beginning of a line),
'$' (matching the null string at the end of a line),
[= Anker]
a '\' followed by one of the characters "^.[$()|*+?{\"
(matching that character taken as an ordinary character),
a '\' followed by any other character(!) (matching that
character taken as an ordinary character, as if the '\'
had not been present(!)),
[= escapetes Metazeichen]
or a single character with no
other significance (matching that character).
[= literales Zeichen]
A '{' fol‐
lowed by a character other than a digit is an ordinary
character, not the beginning of a bound(!). It is ille‐
gal to end an RE with '\'.
A bracket expression is a list of characters enclosed in
"[]". It normally matches any single character from the
list (but see below). If the list begins with '^', it
matches any single character (but see below) not from the
rest of the list. If two characters in the list are sep‐
arated by '-', this is shorthand for the full range of
characters between those two (inclusive) in the collating
sequence, for example, "[0-9]" in ASCII matches any deci‐
mal digit. It is illegal(!) for two ranges to share an
endpoint, for example, "a-c-e". Ranges are very collat‐
ing-sequence-dependent, and portable programs should
avoid relying on them.
[= Zeichenklasse]
To include a literal ']' in the list, make it the first
character (following a possible '^'). To include a lit‐
eral '-', make it the first or last character, or the
second endpoint of a range. To use a literal '-' as the
first endpoint of a range, enclose it in "[." and ".]"
to make it a collating element (see below). With the
exception of these and some combinations using '[' (see
next paragraphs), all other special characters, including
'\', lose their special significance within a bracket
expression.
[= Zeichenklasse]
Within a bracket expression, a collating element (a char‐
acter, a multicharacter sequence that collates as if it
were a single character, or a collating-sequence name for
either) enclosed in "[." and ".]" stands for the sequence
of characters of that collating element. The sequence is
a single element of the bracket expression's list. A
bracket expression containing a multicharacter collating
element can thus match more than one character, for exam‐
ple, if the collating sequence includes a "ch" collating
element, then the RE "[[.ch.]]*c" matches the first five
characters of "chchcc".
[= Collating Symbol (weggelassen)]
Within a bracket expression, a collating element enclosed
in "[=" and "=]" is an equivalence class, standing for
the sequences of characters of all collating elements
equivalent to that one, including itself. (If there are
no other equivalent collating elements, the treatment is
as if the enclosing delimiters were "[." and ".]".) For
example, if o and ^ are the members of an equivalence
class, then "[[=o=]]", "[[=^=]]", and "[o^]" are all syn‐
onymous. An equivalence class may not(!) be an endpoint
of a range.
[= Equivalence Class Expression (weggelassen)]
Within a bracket expression, the name of a character
class enclosed in "[:" and ":]" stands for the list of
all characters belonging to that class. Standard charac‐
ter class names are:
alnum digit punct
alpha graph space
blank lower upper
cntrl print xdigit
These stand for the character classes defined in
wctype(3). A locale may provide others. A character
class may not be used as an endpoint of a range.
[= Zeichenklassenausdruecke]
In the event that an RE could match more than one sub‐
string of a given string, the RE matches the one starting
earliest in the string. If the RE could match more than
one substring starting at that point, it matches the
longest. Subexpressions also match the longest possible
substrings, subject to the constraint that the whole
match be as long as possible, with subexpressions start‐
ing earlier in the RE taking priority over ones starting
later. Note that higher-level subexpressions thus take
priority over their lower-level component subexpressions.
[= Gierigkeit; Der laengste der fruehesten Treffer]
Match lengths are measured in characters, not collating
elements. A null string is considered longer than no
match at all. For example, "bb*" matches the three mid‐
dle characters of "abbbc", "(wee|week)(knights|nights)"
matches all ten characters of "weeknights", when "(.*).*"
is matched against "abc" the parenthesized subexpression
matches all three characters, and when "(a*)*" is matched
against "bc" both the whole RE and the parenthesized sub‐
expression match the null string.
[= Gierigkeit; Der laengste der fruehesten Treffer]
Moeglicherweise weitere erwartete Features (wie beispielsweise die Wortgrenzenanker \< \>) sind nicht Teil von EREs (was schon daran ersichtlich ist, dass bei EREs alle Metazeichen unescapet sind ). Dass sie dennoch bei egrep funktionieren, liegt daran, dass egrep nicht strikt POSIX entspricht, sondern verschiedene Erweiterungen und Abweichungen hat. Auf solche und weitere Features werden wir in spaeteren Kapiteln dieses Kurses noch eingehen. Das Kapitel EREs wird hiermit erstmal geschlossen.
Aufgaben:
1) Schreibe eine RE die immer matcht.
2) Schreibe eine RE die niemals matcht.
3) Schreibe eine RE die leere Zeilen matcht.
4) Filtere alle leere Zeilen weg.
5) Wie kann der Circumflex den Zeilenanfang matchen, wenn er nicht als erstes Zeichen in der RE steht?