Wörtliche Charaktere
Der einfachste reguläre Ausdruck besteht aus einem einzelnen Literalzeichen, z A. Es entspricht dem ersten Vorkommen dieses Zeichens in der Zeichenfolge. Es gibt noch einige andere nicht druckbare Zeichen im Kopf behalten. Die folgende Liste ist nicht vollständig und wir werden über mehr sprechen nicht druckbare Zeichen in der Zukunft:
t tab character (ASCII 0x09)
r carriage return (ASCII 0x0D)
n line feed (ASCII 0x0A)
d Matches any Unicode decimal digit. Contains [0-9] many different digit characters.
D Matches any character which isn't a decimal digit. Reverse of /d
s Matches all whitespace characters
S Matches characters that aren't whitespace.
w Matches phrase characters together with alphanumeric characters.
If the ASCII flag is used, solely [a-zA-Z0-9_] is matched.
W Matches any character which isn't a phrase character.
That is the other of w.
b phrase boundary
B Not a phrase boundary
^ Starting of a string
$ Finish of a string
A Matches solely initially of the string.
Z Matches solely on the finish of the string.
Die Lösung besteht darin, die reine String-Notation von Python für Muster regulärer Ausdrücke zu verwenden; Backslashes werden in einem String-Literal mit dem Präfix nicht auf besondere Weise behandelt 'r'
. So r"n"
ist eine aus zwei Zeichen bestehende Zeichenfolge, die Folgendes enthält: ''
Und 'n'
während "n"
ist eine einstellige Zeichenfolge, die eine neue Zeile enthält.
Spezielle Charaktere
Es gibt 12 Sonderzeichen die reserviert sind, damit wir auf mehr Muster in Regex zugreifen können. Diese Sonderzeichen werden oft aufgerufen „Metazeichen“. Die meisten davon sind Fehler, wenn man sie alleine verwendet.
. (interval or dot) Within the default mode, this matches any character
besides a brand new line. If the DOTALL flag has been specified,
this matches any character together with a newline.
(Backslash) Entweder maskiert Sonderzeichen (damit Sie Zeichen wie „*“, „?“) abgleichen können,
oder signalisiert eine besondere Sequenz^ (Caret) Entspricht dem Anfang der Zeichenfolge und im MULTILINE-Modus
stimmt auch unmittelbar nach jedem Zeilenumbruch überein.$ (Dollarzeichen) Entspricht dem Ende der Zeichenfolge oder kurz davor
newline am Ende der Zeichenfolge und in MULTILINE
mode passt auch vor einem Zeilenumbruch| (vertikaler Balken) A|B, wobei A und B beliebige REs sein können, erstellt eine reguläre
Ausdruck, der entweder mit A oder B übereinstimmt.? (Fragezeichen) Bewirkt, dass das resultierende RE mit 0 oder 1 Wiederholungen von übereinstimmt
das vorhergehende RE. ab? stimmt entweder mit „a“ oder „ab“ überein.* (Sternchen) Bewirkt, dass die resultierende RE mit 0 oder mehr Wiederholungen übereinstimmt
des vorhergehenden RE, so viele Wiederholungen wie möglich.
ab* entspricht „a“, „ab“ oder „a“, gefolgt von „b“.+ (Pluszeichen) Bewirkt, dass das resultierende RE mit einer oder mehreren Wiederholungen von übereinstimmt
das vorhergehende RE. ab+ stimmt mit „a“ gefolgt von „any“ überein
Nicht-Null-Anzahl von 'b's; es wird nicht nur mit „a“ übereinstimmen.(,) (Klammer) Entspricht jedem regulären Ausdruck in der
Klammern und gibt den Anfang und das Ende einer Gruppe an;
Der Inhalt einer Gruppe kann nach einem Match abgerufen werden
wurde durchgeführt und kann später in der Zeichenfolge abgeglichen werden[ (square bracket) Used to indicate a set of characters{ (curly brace) Specifies the number of copies of previous RE to be matched;
Quantifiers — * + ? and {}
- * match 0 or more repetitions of the preceding RE
- +. match 1 or more repetitions of the preceding RE
- ? causes the resulting RE to match 0 or 1 repetitions of the preceding RE
- {m} Specifies that exactly m copies of the previous RE should be matched
- {m, n} causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible
abc* matches a string that has ab followed by zero or more c
abc+ matches a string that has ab followed by one or more c
abc? matches a string that has ab followed by zero or one c
abc{2} matches a string that has ab followed by 2 c
abc{2,} matches a string that has ab followed by 2 or more c
abc{2,5} matches a string that has ab followed by 2 up to 5 c
a(bc)* matches a string that has a followed by zero or more copies of the sequence bc
a(bc){2,5} matches a string that has a followed by 2 up to 5 copies of the sequence bc
Greedy and Lazy match
The quantifiers ( * + {}
) are greedy operators, so they expand the match as far as they can through the provided text.
For example, <.+>
matches <div>simple div</div>
in This is a <div> simple div</div> test
.
In order to catch only the div
tag we can use a ?
to make it lazy:
<.+?> matches any character one or more times included
inside < and >
Notice that a better solution should avoid the usage of .
in favor of a more strict regex:
<[^<>]+> wie <.+?>, aber strenger
Anker – ^ und $
^The matches any string that begins with "The"
finish$ matches a string that ends with "finish"
^The top$ begins with "The top" and ends with "The top" (actual match)
roar matches any string that has the textual content roar in it
Ein gutes Beispiel könnte darin bestehen, zu überprüfen, ob die Zeichenfolge Dezimalzahlen enthält ^d+$.
d+ sagt to match, dass alle Zahlen Dezimalzahlen sein sollten, aber das würde auch mit abcd4cd übereinstimmen. Deshalb möchten wir davor ^ und danach $ hinzufügen, um eine exakte Übereinstimmung zu erzielen.
Gruppieren und Erfassen
- Beispiele für Klammern – Entspricht dem genauen regulären Ausdruck in den Klammern
1. a(bc) parentheses create a capturing group with worth bc
2. a(?:bc)* utilizing ?: we disable the capturing group
3. a(?<foo>bc) utilizing ?<foo> we put a reputation to the group
- Klammerausdrücke – Gibt „true“ zurück, wenn RE mit einem der regulären Ausdrücke in den Klammern übereinstimmt.
[abc] matches a string that has both an a or ab or ac
[a-c] similar as earlier
[a-fA-F0-9] a string that represents a single hexadecimal digit, case insensitively
[0-9]% a string that has a personality from 0 to 9 earlier than a % signal
[^a-zA-Z] a string that has not a letter from a to z or from A to Z. (^ is used as negation)
Grenzen – b und B
b
stellt eine dar Anker wie Caret (das ist vergleichbar mit $
Und ^
) passende Positionen wo Eine Seite ist ein Wort Charakter (wie w
) und das andere Seite ist kein Wort Charakter (Beispielsweise kann es der Anfang der Zeichenfolge oder ein Leerzeichen sein).
babcb performs a "entire phrases solely" search
Es kommt mit Negation, B
. Dies entspricht allen Positionen wo b
stimmt nicht überein und könnte der Fall sein, wenn wir ein Suchmuster finden möchten, das vollständig von Wortzeichen umgeben ist.
BabcB matches provided that sample is absolutely surrounded by phrase characters