|
Syntax of Regular Expressions Regulärer Ausdruck sehen für Anfänger oft verwirrend aus, aber sie sind wirklich simpel, handlich und ein mächtiges Werkzeug. Einige Beispiele:
Alle Überschriftszeilen: (?i)<h[\d]*> Reelle Zahlen (Beispiel '13.88e-4', '-7E2'): ([+\-]?\d+(\.\d+)?([eE][+\-]?\d+)?) Telefonnummern (Beispiel '+7(812) 555-5555', '(20)555-55-55', '555-5555'): ((\+\d *)?(\(\d{2,4}\) *)?\d{3}(-\d*)*) e-Mail Adressen (Beispiel 'anso@mail.com', 'anso@mailbox.alkor.com'): ([_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+) Internet Adresse url(beispiel 'http://www.paycash.com', 'ftp://195.5.138.172\default.htm'): ([Ff][Tt][Pp]|[Hh][Tt][Tt][Pp])://([_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+))((/[ _a-zA-Z\d\-\\\.]+)+)* Detaillierte Erklärung
Jedes Einzelzeichen stellt sich selber dar, es sei denn es ist ein Metazeichen mit einer unten beschriebenen, speziellen Bedeutung.
Eine Serie von Charaktern passen zum Ziel String. Z.B. die Buchstaben "bluh" passen zu "bluh" im Ziel String. Simpel, oder?
Sie können erzwingen, dass Charakter, die normalerweise als Metazeichen interpretiert werden, als Buchstaben interpretiert werden, indem Sie diese mit dem Prefix "\" maskieren. Zum Beispiel, "^" kennzeichnet den Beginn eines String, aber "\^ " kennzeichnet den Charakter als Buchstaben " ^" "\\" kennzeichnet "\" usw.
Characters may be specified using a metacharacter syntax much like that used in C: ``\n'' matches a newline, ``\t'' a tab, ``\r'' a carriage return, ``\f'' a form feed, etc. More generally, \xnn, where nn is a string of hexadecimal digits, matches the character whose ASCII value is nn. If you need wide (UniCode) character code, you can use '\x{nnnn}', where 'nnnn' - one or more hexadedimal digits.
You can specify a character class, by enclosing a list of characters in [], which will match any one character from the list. If the first character after the ``['' is ``^'', the class matches any character not in the list.
Within a list, the ``-'' character is used to specify a range, so that a-z represents all characters between ``a'' and ``z'', inclusive. If you want ``-'' itself to be a member of a class, put it at the start or end of the list, or escape it with a backslash.
The following all specify the same class of three characters: [-az], [az-], and [a\-z]. All are different from [a-z], which specifies a class containing twenty-six characters. If you want ']' you may place it at the start of list or escape it with a backslash.
Examples of queer ;) ranges: [\n-\x0D] match any of #10,#11,#12,#13. [\d-t] match any digit, '-' or 't'. []-a] match any char from ']'..'a'.
Finally, the ``.'' metacharacter matches any character except ``\n'' (unless you use /s modifier - see below. Note: /s is set by default.
You can specify a series of alternatives for a pattern using ``|'' to separate them, so that fee|fie|foe will match any of ``fee'', ``fie'', or ``foe'' in the target string (as would f(e|i|o)e). The first alternative includes everything from the last pattern delimiter (``('', ``['', or the beginning of the pattern) up to the first ``|'', and the last alternative contains everything from the last ``|'' to the next pattern delimiter. For this reason, it's common practice to include alternatives in parentheses, to minimize confusion about where they start and end.
Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when mathing foo|foot against ``barefoot'', only the ``foo'' part will match, as that is the first alternative tried, and it successfully matches the target string. (This might not seem important, but it is important when you are capturing matched text using parentheses.)
Also remember that ``|'' is interpreted as a literal within square brackets, so if you write [fee|fie|foe] you're really only matching [feio|].
The bracketing construct ( ... ) may also be used for define r.e. subexpressions. Subexpressions are numbered based on the left to right order of their opening parenthesis.
First subexpression has number '1'
Any item of a regular expression may be followed with digits in curly brackets of the form {n,m}, where n gives the minimum number of times to match the item and m gives the maximum. The form {n} is equivalent to {n,n} and matches exactly n times. The form {n,} matches n or more times. (If a curly bracket occurs in any other context, it is treated as a regular character.) The * modifier is equivalent to {0,}, the + modifier to {1,} and the ? modifier to {0,1}. There is no limit to the size of n or m, but large numbers will chew up more memory and slow down r.e. execution.
Short list of metacharacters
You may use \w, \d and \s within character classes.
\1 through \9 are interpreted as backreferences. \<n> matches previously matched subexpression #<n>. For example: '(.)\1+' match 'aaaa' and 'cc'. '(.+)\1+' also match 'abab' and '123123'. See examples in demo project
By default, the ^ character is only guaranteed to match at the beginning of the string, the $ character only at the end (or before the newline at the end) and perl does certain optimizations with the assumption that the string contains only one line. Embedded newlines will not be matched by ``^'' or ``$''.
You may, however, wish to treat a string as a multi-line buffer, such that the ``^'' will match after any newline within the string, and ``$'' will match before any newline. At the cost of a little more overhead, you can do this by using the m modifier on the pattern match operator.
To facilitate multi-line substitutions, the ``.'' character never matches a newline unless you use the s modifier. List of modifiers
Perl extensions
(?imsxr-imsxr)
You may use it into r.e. for modifying modifiers by the fly, for example
(?i)New York
will match string 'New york' and 'New York', but
(?i)New (?-i)York
will match only 'New York'
If this construction inlined into subexpression, then it effects only into this subexpression
(?i)(New )?York
will match 'New york' and 'new york' , but
((?i)New )?York
will match 'new York', but not 'new york'
(?#text)
A comment. The text is ignored. If the x switch is used to enable whitespace formatting, a simple # will suffice. |
| Converted from CHM to HTML with chm2web Pro 2.74 (unicode) |