Englisch Русский Deutsch Български Français Spanisch

TRegExpr

Implements regular expressions in pure Pascal. Compatible with Free Pascal, Delphi 2-7, C++Builder 3-6.

To use it, copy files „regexpr.pas“, „regexpr_unicodedata.pas“, „regexpr_compilers.inc“, to your project folder.

The library is already included into Lazarus (Free Pascal) project so you do not need to copy anything if you use Lazarus.

Klasse TRegExpr

Haupt- und Nebenversion

Return major and minor version of the component.

VersionMajor = 1
VersionMinor = 101

Ausdruck

Ein regulärer Ausdruck.

For optimization, regular expression is automatically compiled into P-code. Human-readable form of the P-code is returned by Dump.

In case of any errors in compilation, Error method is called (by default Error raises exception ERegExpr).

ModifierStr

Werte für reguläre Ausdrücke setzen oder auslesen.

Format of the string is similar to (?ismx-ismx). For example ModifierStr := ‘i-x’ will switch on the modifier /i, switch off /x and leave unchanged others.

Wenn Sie versuchen, einen nicht unterstützten Modifikator einzustellen, wird Error aufgerufen.

ModifierI

Modifier /i, „case-insensitive“, initialized with RegExprModifierI value.

ModifierR

Modifier /r, “ russische Zeichenbereiche“, initialisiert mit dem Wert von RegExprModifierR.

ModifierS

Modifier /s, „einzeilige Zeichenketten „, initialisiert mit dem Wert von RegExprModifierS .

ModifierG

Modifier /g, „Gier (greediness)“, initialisiert mit dem Wert von RegExprModifierG .

ModifierM

Modifier /m, „mehrzeilige Zeichenkette“, initialisiert mit dem Wert von RegExprModifierM .

ModifierX

Modifier /x, „erweiterte Syntax“, initialisiert mit dem Wert von RegExprModifierX .

Exec

Finds regular expression against AInputString, starting from the beginning.

The overloaded Exec version without AInputString exists, it uses AInputString from previous call.

Siehe auch die globale Funktion ExecRegExpr, die sich ohne explizite Erstellung eines TRegExpr-Objekts nutzen lässt.

ExecNext

Finds next match. If parameter ABackward is True, it goes downto position 1, ie runs backward search.

Without parameter it works the same as:

if MatchLen [0] = 0
  then ExecPos (MatchPos [0] + 1)
  else ExecPos (MatchPos [0] + MatchLen [0]);

Löst eine Ausnahme aus, falls sie ohne vorherigen erfolgreichen Aufruf von Exec, ExecPos oder ExecNext verwendet wird.

So you always must use something like:

if Exec(InputString)
  then
    repeat
      { proceed results}
    until not ExecNext;

ExecPos

Finds match for AInputString starting from AOffset position (1-based).

Parameter ABackward means going from AOffset downto 1, ie backward search.

Parameter ATryOnce means that testing for regex will be only at the initial position, without going to next/previous positions.

InputString

Gibt die aktuelle Eingabezeichenfolge zurück (aus dem letzten Exec-Aufruf oder der letzten Zuweisung an diese Eigenschaft stammend).

Jede Zuweisung zu dieser Eigenschaft setzt Match, MatchPos und MatchLen zurück.

Substitute

function Substitute (const ATemplate: RegExprString): RegExprString;

Returns ATemplate, where $& or $0 are replaced with the found match, and $1 to $9 are replaced with found groups 1 to 9.

To use in template the characters $ or \, escape them with a backslash \, like \\ or \$.

Symbol Description
$& ganze Übereinstimmung des regulären Ausdrucks
$0 ganze Übereinstimmung des regulären Ausdrucks
$1 .. $9 contents of numbered group 1 .. 9
\n in Windows durch \r\n ersetzt
\l lowercase one next char
\L Kleinbuchstaben alle Zeichen danach
\u uppercase one next char
\U Großbuchstaben alle Zeichen danach
'1\$ is $2\\rub\\' -> '1$ is <Match[2]>\rub\'
'\U$1\\r' wird zu '<Match[1] in uppercase>\r'

Wenn Sie eine Ziffer unmittelbar hinter $n schreiben möchten, müssen Sie n mit geschweiften Klammern {} abgrenzen.

'a$12bc' -> 'a<Match[12]>bc'
'a${1}2bc' -> 'a<Match[1]>2bc'.

To use found named groups, use syntax ${name}, where „name“ is valid identifier of previously found named group (starting with non-digit).

Split

Splits AInputStr into APieces by regex occurrences.

Ruft intern Exec / ExecNext auf

Siehe alternativ die globale Funktion SplitRegExpr, die sich verwenden läßt, ohne explizit ein``TRegExpr``-Objekt zu erstellen.

Replace, ReplaceEx

function Replace (Const AInputStr : RegExprString;
  const AReplaceStr : RegExprString;
  AUseSubstitution : boolean= False)
 : RegExprString; overload;

function Replace (Const AInputStr : RegExprString;
  AReplaceFunc : TRegExprReplaceFunction)
 : RegExprString; overload;

function ReplaceEx (Const AInputStr : RegExprString;
  AReplaceFunc : TRegExprReplaceFunction):
  RegExprString;

Returns the string with regex occurencies replaced by the replace string.

Wenn das letzte Argument (&quot;AUseSubstitution&quot;) wahr ist, wird &quot;AReplaceStr&quot; als Vorlage für Substitutionsmethoden verwendet.

Ausdruck: = &#39;((? I) block | var) \ s * (\ s * \ ([^] * \) \ s *) \ s *&#39;; Ersetzen Sie (&#39;BLOCK (test1)&#39;, &#39;def &quot;$ 1&quot; -Wert &quot;$ 2&quot;&#39;, True);

Liefert ``def „BLOCK“ Wert &quot;test1&quot; ``

Ersetzen (&#39;BLOCK (test1)&#39;, &#39;def &quot;$ 1&quot; Wert &quot;$ 2&quot;&#39;, False)

Liefert `` def &quot;$ 1&quot; Wert &quot;$ 2&quot; ``

Ruft intern Exec / ExecNext auf

Overloaded version and ReplaceEx operate with callback function, so you can implement really complex functionality.

Siehe auch die globale Funktion ReplaceRegExpr, die Sie ohne explizite TRegExpr-Objekterstellung verwenden können.

SubExprMatchCount

Count of groups (subexpressions) found in last Exec / ExecNext call.

If there are no groups found, but some string was found (Exec* returned True), it returns 0. If no groups nor some string were found (Exec / ExecNext returned false), it returns -1.

Note, that some group may be not found, and for such group MathPos=MatchLen=-1 and Match=’’.

Ausdruck: = &#39;(1) 2 (3)&#39; &#39;; Exec (&#39;123&#39;): SubExprMatchCount = 2, Match [0] = &#39;123&#39;, [1] = &#39;1&#39;, [2] = &#39;3&#39; Exec (&#39;12&#39;): SubExprMatchCount = 1, Match [0] = &#39;12 &#39;, [1] =&#39; 1 &#39;Exec (&#39; 23 &#39;): SubExprMatchCount = 2, Match [0] =&#39; 23 &#39;, [1] =&#39; &#39;, [2] =&#39; 3 &#39;Exec (&#39; 2 &#39;): SubExprMatchCount = 0, Match [0] =&#39; 2 &#39;Exec (&#39; 7 &#39;) - Rückgabe False: SubExprMatchCount = -1

MatchPos

Position (1-based) of group with specified index. Result is valid only after some match was found. First group has index 1, the entire match has index 0.

Returns -1 if no group with specified index was found.

MatchLen

Length of group with specified index. Result is valid only after some match was found. First group has index 1, the entire match has index 0.

Returns -1 if no group with specified index was found.

Spiel

String of group with specified index. First group has index 1, the entire match has index 0. Returns empty string, if no such group was found.

MatchIndexFromName

Returns group index (1-based) from group name, which is needed for „named groups“. Returns -1 if no such named group was found.

LastError

Returns Id of last error, or 0 if no errors occured (unusable if Error method raises exception). It also clears internal status to 0 (no errors).

ErrorMsg

Gibt die Error-Nachricht für einen Fehler mit `` ID = AErrorID`` zurück.

CompilerErrorPos

Returns position in regex, where P-code compilation was stopped.

Useful for error diagnostics.

SpaceChars

Contains chars, treated as \s (initially filled with RegExprSpaceChars global constant).

WordChars

Contains chars, treated as \w (initially filled with RegExprWordChars global constant).

LineSeparators

Line separators (like \n in Unix), initially filled with RegExprLineSeparators global constant).

See also Line Boundaries

UseLinePairedBreak

Boolean property, enables to detect paired line separator CR LF.

See also Line Boundaries

For example, if you need only Unix-style separator LF, assign LineSeparators := #$a and UseLinePairedBreak := False.

If you want to accept as line separators only CR LF but not CR or LF alone, then assign LineSeparators := '' (empty string) and UseLinePairedBreak := True.

By default, „mixed“ mode is used (defined in RegExprLineSeparators global constant):

LineSeparators := #$d#$a;
UseLinePairedBreak := True;

Behaviour of this mode is described in the Line Boundaries.

Kompilieren

Compiles regular expression to internal P-code.

Nützlich zum Beispiel für GUI-Editoren für reguläre Ausdrücke, um reguläre Ausdrücke zu überprüfen, ohne ihn zu verwenden.

Dump

Shows P-code (compiled regular expression) as human-readable string.

Globale Konstanten

EscChar

Escape character, by default backslash '\'.

SubstituteGroupChar

Char used to prefix groups (numbered and named) in Substitute method, by default '$'.

RegExprModifierI

Modifier i default value.

RegExprModifierR

Modifier r default value.

RegExprModifierS

Modifier s default value.

RegExprModifierG

Modifier g default value.

RegExprModifierM

Modifier m default value.

RegExprModifierX

Modifier x default value.

RegExprSpaceChars

Default for SpaceChars property.

RegExprWordChars

Default value for WordChars property.

RegExprLineSeparators

Default value for LineSeparators property.

Globale Funktionen

Alle diese Funktionen stehen als Methoden von TRegExpr zur Verfügung, aber mit globalen Funktionen müssen Sie keine TReExpr-Instanz erstellen, sodass Ihr Code einfacher wäre, wenn Sie nur eine Funktion benötigen.

ExecRegExpr

Returns True if the string matches the regular expression. Just like Exec in TRegExpr.

SplitRegExpr

Splits the string by regular expression occurences. See also Split if you prefer to create TRegExpr instance explicitly.

ReplaceRegExpr

Funktion ReplaceRegExpr (const ARegExpr, AInputStr, AReplaceStr: RegExprString; AUseSubstitution: boolean = False): RegExprString; Überlast; Typ TRegexReplaceOption = (rroModifierI, rroModifierR, rroModifierS, rroModifierG, rroModifierM, rroModifierX, rroUseSubstitution, rroUseOsLineEnd); TRegexReplaceOptions = Set von TRegexReplaceOption; Funktion ReplaceRegExpr (const ARegExpr, AInputStr, AReplaceStr: RegExprString; Optionen: TRegexReplaceOptions): RegExprString; Überlast;

Gibt den String mit regulären Ausdrücken zurück, die durch AReplaceStr ersetzt werden. Siehe auch Replace, wenn Sie es vorziehen, die TRegExpr-Instanz explizit zu erstellen.

If last argument (AUseSubstitution) is True, then AReplaceStr will be used as template for Substitution methods:

ReplaceRegExpr (&#39;((?)) Block | var) \ s * (\ s * \ ([^] * \) \ s *) \ s *&#39;, &#39;BLOCK (test1)&#39;, &#39;def &quot;$ 1&quot; value &quot; $ 2 &quot;, wahr)

Returns def 'BLOCK' value 'test1'

Aber dieses hier (Anmerkung: Es gibt kein letztes Argument):

ReplaceRegExpr (&#39;((?)) Block | var) \ s * (\ s * \ ([^] * \) \ s *) \ s *&#39;, &#39;BLOCK (test1)&#39;, &#39;def &quot;$ 1&quot; value &quot; $ 2 &quot;&#39;)

Liefert `` def &quot;$ 1&quot; Wert &quot;$ 2&quot; ``

Version mit Optionen

Mit Options steuert man das Verhalten von \n (mit der Option rroUseOsLineEnd wird jedes \n unter Windows durch \n\r ersetzt, unter Linux bleibt es ein \n). Und so weiter.

Typ TRegexReplaceOption = (rroModifierI, rroModifierR, rroModifierS, rroModifierG, rroModifierM, rroModifierX, rroUseSubstitution, rroUseOsLineEnd);

QuoteRegExprMetaChars

Replace all metachars with its safe representation, for example abc'cd.( is converted to abc\'cd\.\(

This function is useful for regex auto-generation from user input.

RegExprSubExpressions

Makes list of subexpressions found in ARegExpr.

In ASubExps every item represents subexpression, from first to last, in format:

String - Unterausdruck (ohne '()')

 Low word of Object - starting position in ARegExpr, including ‘(’ if exists! (first position is 1)

 High word of Object - length, including starting ‘(’ and ending ‘)’ if exist!

AExtendedSyntax - must be True if modifier /x os on, while using the regex.

Usefull for GUI editors of regex (you can find example of usage in REStudioMain.pas)

Ergebniscode Bedeutung
0 Success. No unbalanced brackets were found.
-1 Not enough closing brackets ).
-(n+1) At position n it was found opening [ without corresponding closing ].
n At position n it was found closing bracket ) without corresponding opening (.

If Result <> 0, then ASubExprs can contain empty items or illegal ones.

ERegExpr

ERegExpr = Klasse (Ausnahme) public ErrorCode: integer; // Fehlercode. Kompilierungsfehlercodes liegen vor 1000 CompilerErrorPos: integer; // Position in re, an der der Kompilierungsfehler aufgetreten ist end;

Unicode

In Unicode mode, all strings (InputString, Expression, internal strings) are of type UnicodeString/WideString, instead of simple „string“. Unicode slows down performance, so use it only if you really need Unicode support.

To use Unicode, uncomment {$DEFINE UniCode} in regexpr.pas (remove off).