Sie sind auf Seite 1von 2

Regular expressions

Pgina 1 de 2

Regular expressions
Regular expressions are a very powerful search tool. They allow to search for complex classes of words.
Regular expressions are mainly meant for professionals, but can also be useful in the office for finding certain
documents (see examples below).
Total Commander supports regular expressions in the following functions:
- Commands - Search (in file name and file contents)
- In Lister
- In the Multi-Rename tool
- In the selection dialog
Regular expressions consist of normal characters and special characters, so-called meta-characters. The
following characters are meta-characters or initial parts of meta-characters:
. \ ( ) [ ] { } ^ $ + * ? (only in character classes: - )
Normal characters:
test

finds the string "test" in the searched text. Note: This finds "test" ANYWHERE in a file name or on a line
in text.

Escape sequences:
A backslash \ starts an Escape sequence. Examples for escape sequences:
Tabstop
\t
\xnn Character with hexadecimal code nn. Example: \x20 is the space character. The character table
charmap.exe (if installed) shows the character code of most special characters. You can use the
Windows calculator in scientific mode to convert from decimal to hex.
\x{nnnn}
Unicode character with hexadecimal code nn. Note that Total Commander now uses Unicode for file
names, so you need to use this notation for characters other than basic English.
Left square bracket. Since the square brackets are meta-characters, they need to be written as \[ to
\[
search for them in the target string.
Finds a backslash.
\\
Finds a dot ("." alone finds any character, see below).
\.
Character classes
Characters in square brackets build a character class. It will find exacly one character from this class. A dash
allows to define groups, e.g. [a-z]. A ^ at the beginning finds all characters except for those listed.
Examples:
Finds exactly one of the listed vovels.
[aeiou]
Finds everything except for a vovel.
[^aeiou]
M[ae][iy]er Finds a Mr. Meier in all possible ways of writing: Mayer, Meyer, Maier, Meier. Very useful if you
cannot remember the exact writing of a name.
Meta-characters
Here is a list of the most important meta-characters:
^
Line start
$
Line end
.
Any character
a letter, digit or underscore _
\w
the opposite of \w
\W
\d
a digit
no digit
\D
a word separator (space, tab etc)
\s
\S
no word separator
\b
finds a word boundary (combination of \s and \S)
the opposite of \b
\B
Iterators
Iterators are used for a repetition of the character or expression to the left of the iterator.
*
zero or more occurances
one or more occurances

mk:@MSITStore:D:\PORTABLE\TotalCmd\TOTALCMD.CHM::/reg_ex.htm

02/07/2011

Regular expressions

Pgina 2 de 2

+
one or more occurances
{n}
exactly n occurances
{n,}
at least n occurances
{n,m}
at least n and max. m occurances
All these operators are "greedy", which means that they take as many characters as they can get. Putting a
question mark ? after an operator makes it "non-greedy", i.e. it takes only as many characters as needed.
Example: "b+" applied to the target string "abbbbc" finds "bbbb", "b+?" finds just "b".
Alternatives
Alternatives are put in round braces, and are separated by a vertical dash.
Example: (John|James|Peter) finds one of the names John, James or Peter.
Subexpressions for search+replace
Text parts in round braces are taken as subexpressions.
Example: To swap the title and interpret in the file name of an mp3 file, when they are separated by a dash
(Title - Interpret.mp3), this can be solved like this:
Search for: (.*) - (.*)\.mp3
Replace by: $2 - $1.mp3
Here $1 means the text in the first brace, and $2 the text in the second brace.
Backreferences
\n
Finds subexpression n another time in the search result.
Example: (.+)\1+ finds e.g. abab (where the first ab is found by .+ and the second by \1+ )
Modifiers
Modifiers are used for changing behaviour of regular expressions.
(?i)
(?-i)
(?g)
(?-g)

Ignore Upper-/lowercase. In Total Commander, this is the default for file names.
Case-sensitive matching.
Switches on "greedy" mode (active by default)
Turns off "greedy" mode, so "+" means the same as "+?"

The other modificators are not relevant for Total Commander, because the program only supports searching
within one line.
Total Commander uses the free Delphi library TRegExpr by Andrey V. Sorokin:
http://www.regexpstudio.com/
Some of the above explanations are from the help file for this library.

mk:@MSITStore:D:\PORTABLE\TotalCmd\TOTALCMD.CHM::/reg_ex.htm

02/07/2011

Das könnte Ihnen auch gefallen