Difference between revisions of "ScriptCore3.ExecRegExpr"
m (→Description) |
m (→Script Code) |
||
Line 18: | Line 18: | ||
TRegTest = RECORD | TRegTest = RECORD | ||
Reg: String; | Reg: String; | ||
− | + | Str: TStringList; | |
end; | end; | ||
Line 32: | Line 32: | ||
begin | begin | ||
if RegTest[i].Reg = RegExpr then | if RegTest[i].Reg = RegExpr then | ||
− | + | begin | |
− | + | found := TRUE; | |
− | + | index := i; | |
− | + | break; | |
− | + | end; | |
end; | end; | ||
// add new TRegTest if not found | // add new TRegTest if not found | ||
Line 42: | Line 42: | ||
begin | begin | ||
len := GetArrayLength(RegTest); | len := GetArrayLength(RegTest); | ||
− | + | SetArrayLength(RegTest,len+1); | |
− | + | index := GetArrayLength(RegTest)-1; | |
− | + | RegTest[index].Str := File.CreateStringList; | |
− | + | RegTest[index].Reg := RegExpr; | |
end; | end; | ||
// finally add string | // finally add string | ||
Line 65: | Line 65: | ||
begin | begin | ||
WriteLn(' '+RegTest[i].Reg); | WriteLn(' '+RegTest[i].Reg); | ||
− | + | for j:=0 to RegTest[i].Str.Count-1 do | |
− | + | WriteLn(' '+iif(ExecRegExpr(RegTest[i].Reg,RegTest[i].Str[j]),'TRUE ','FALSE')+' '+RegTest[i].Str[j]); | |
− | + | WriteLn(''); | |
end; | end; | ||
WriteLn('-- ExecRegExpr Test Stop --'); | WriteLn('-- ExecRegExpr Test Stop --'); | ||
Line 92: | Line 92: | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
===Console output=== | ===Console output=== | ||
<pre> | <pre> |
Latest revision as of 14:08, 25 August 2013
function ExecRegExpr(const ARegExpr, AInputStr: string): boolean ARegExpr: Regular Expression AInputStr: Input string Result: True if AInputString matches reg.exp. ARegExpr
Description
Function checks if string AInputString matches regular expression ARegExpr
Exception will not be raised if there are syntax errors in ARegExpr.
For detailed information about Regular Expressions see Regular_Expression on Wikipedia
Example
Below example provides a script to test multiple ExecRegExpr calls.
Script Code
type
TRegTest = RECORD
Reg: String;
Str: TStringList;
end;
var
RegTest: array of TRegTest;
i: integer;
procedure AddTest(RegExpr,Input: string);
var found: boolean; index,len: integer;
begin
// maybe this regexpr is already there
for i:=0 to GetArrayLength(RegTest)-1 do
begin
if RegTest[i].Reg = RegExpr then
begin
found := TRUE;
index := i;
break;
end;
end;
// add new TRegTest if not found
if not found then
begin
len := GetArrayLength(RegTest);
SetArrayLength(RegTest,len+1);
index := GetArrayLength(RegTest)-1;
RegTest[index].Str := File.CreateStringList;
RegTest[index].Reg := RegExpr;
end;
// finally add string
RegTest[index].Str.Append(Input);
end;
procedure FreeMem();
begin
for i:=0 to GetArrayLength(RegTest)-1 do
RegTest[i].Str.Free;
end;
procedure RunTests();
var i,j: integer;
begin
WriteLn('-- ExecRegExpr Test Start --');
WriteLn('');
for i:=0 to GetArrayLength(RegTest)-1 do
begin
WriteLn(' '+RegTest[i].Reg);
for j:=0 to RegTest[i].Str.Count-1 do
WriteLn(' '+iif(ExecRegExpr(RegTest[i].Reg,RegTest[i].Str[j]),'TRUE ','FALSE')+' '+RegTest[i].Str[j]);
WriteLn('');
end;
WriteLn('-- ExecRegExpr Test Stop --');
end;
begin
AddTest('/buy (steyr|eagles|hk)','/buy steyr');
AddTest('/buy (steyr|eagles|hk)','/buy hk');
AddTest('/buy (steyr|eagles|hk)','/buy m79');
AddTest('/login [A-Za-z0-9_]*[ \t][A-Za-z0-9_]*$','/login UserNamePassword');
AddTest('/login [A-Za-z0-9_]*[ \t][A-Za-z0-9_]*$','/login UserName Password');
AddTest('/login [A-Za-z0-9_]*[ \t][A-Za-z0-9_]*$','/login User Name Password haha');
AddTest('/login [A-Za-z0-9_]*[ \t][A-Za-z0-9_]*$','/login User_Name01 MyP4sS');
AddTest('/longword [A-Za-z0-9]{10,20}$','/longword 8letters');
AddTest('/longword [A-Za-z0-9]{10,20}$','/longword 10lettersk');
AddTest('/longword [A-Za-z0-9]{10,20}$','/longword 20LettersAreStillOk1');
AddTest('/longword [A-Za-z0-9]{10,20}$','/longword 21LettersAreAlreadyNot');
RunTests();
FreeMem();
end.
Console output
13-08-25 14:54:25 Console Log Started 13-08-25 14:54:26 Welcome to Soldat 1.6.5 13-08-25 14:54:26 [*] Preparing scripts to be launched 13-08-25 14:54:26 [*] [Testing] Compilation started 13-08-25 14:54:26 [*] [Testing] Compilation complete 13-08-25 14:54:26 [*] [Testing] Loading bytecode 13-08-25 14:54:26 [*] [Testing] Bytecode loaded 13-08-25 14:54:26 [*] Done 13-08-25 14:54:26 -- ExecRegExpr Test Start -- 13-08-25 14:54:26 /buy (steyr|eagles|hk) 13-08-25 14:54:26 TRUE /buy steyr 13-08-25 14:54:26 TRUE /buy hk 13-08-25 14:54:26 FALSE /buy m79 13-08-25 14:54:26 /login [A-Za-z0-9_]*[ \t][A-Za-z0-9_]*$ 13-08-25 14:54:26 FALSE /login UserNamePassword 13-08-25 14:54:26 TRUE /login UserName Password 13-08-25 14:54:26 FALSE /login User Name Password haha 13-08-25 14:54:26 TRUE /login User_Name01 MyP4sS 13-08-25 14:54:26 /longword [A-Za-z0-9]{10,20}$ 13-08-25 14:54:26 FALSE /longword 8letters 13-08-25 14:54:26 TRUE /longword 10lettersk 13-08-25 14:54:26 TRUE /longword 20LettersAreStillOk1 13-08-25 14:54:26 FALSE /longword 21LettersAreAlreadyNot 13-08-25 14:54:26 -- ExecRegExpr Test Stop -- 13-08-25 14:54:26 ctf_Ash by chakapoko maker 13-08-25 14:54:26 ASE Port: 23196 13-08-25 14:54:26 Connection for file server started. Port: 23083 13-08-25 14:54:26 Registering server @ ASE Master Server 13-08-25 14:54:26 Registering server @ 67.23.118.179