Program Demo_Many_Regex_Tester2; //
#sign::23 Max: MAXBOX10: 29/02/2020 15:56:10 //#tech:1:23 perf: threads: 6 192.168.1.66 15:56:10 4.7.1.824.7.1.82 //Jedi Code Library also have wrapper around PCRE. Tried it ? – Arioch 'The {I'm struggling to find a way of using Perl compatible regular expressions in a project. The closest I have got is to download TPerlRegEx from http://www.regexbuddy.com/delphi.html - it's a Delphi wrapper around a C++ PCRE library apparently. Test 4 RegEx Libraries: PerlRegex, Synedit, Systools4 , Synapse https://www.regular-expressions.info/delphi.html } //Picture Chart Box --------------------------------------------------------- // Const UrlPictureLookupInfo = 'https://picsum.photos/seed/%d/%d/%d'; REGEXPRESSION1= '[+-]?([0-9]*[.])?[0-9]+'; //float expression REGEXPRESSION2= '[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)'; REGEXPRESSION3= '^\s*[-+]?[0-9]*\.?[0-9]+\s*$'; REGEXPRESSION4= '-?\d*\.?\d+([eE]\-?\d+)?'; procedure PIC_CloseClick(Sender: TObject; var action: TCloseAction); begin //afrm.Close; action:= cafree; writeln('picchartform close at: '+datetimetostr(now)); end; function RunPictureBox(afrm: TForm; const picnr, asizew, asizeh: integer; const UrlPicture,apath: string; save:boolean): string; var pngStream: TMemoryStream; begin pngStream:= TMemoryStream.Create; try HttpGet(Format(UrlPicture,[picnr, asizew, asizeh]), pngStream); writeln('pic size: '+itoa(pngStream.size)) except //if something wrong try using backup server. writeln('PICexcept: '+ExceptionToString(ExceptionType, ExceptionParam)); end; with TLinearBitmap.Create do try pngStream.Position:= 0; LoadFromStream2(pngStream,'JPG'); //or PNG //afrm:= getForm2(asizew+1,asizeh+1, clblack, // 'picsum.photos: '+'Id: '+inttostr(picnr)); afrm.onclose:= @Pic_CloseClick; paintToCanvas(afrm.canvas, Rect(1,1,Width,Height),false); if save then SaveToFile(apath); result:= apath; finally Dispose; Free; pngStream.Free; end; end; procedure SetTEE_Chart; var frm: TForm; chart3: TChart; begin frm:= getForm2(420,320, clblack, 'TEE_Chart'); //sizeable! chart3:= TChart.create(self) with chart3 do begin parent:= frm; //align:= alClient; setBounds(5,5,335,225) title.caption:= 'FRUIT COMPANY'; AddSeries(TBarSeries.Create(Self)); //series[0].Color:= $FF0000; //series[0].ShowInLegend:=false; series[0].Add(3,'Pears', clRed); series[0].Add(4,'Apples', clGreen); series[0].Add(2,'Citros', clyellow); end; if isInternet then RunPictureBox(frm,20,420,320,UrlPictureLookupInfo,'', False); end; procedure ExtractPhones (const AText : string; APhones : TStrings); begin with TRegExpr.Create do try Expression:= '(\+\d *)?(\((\d+)\) *)?(\d+(-\d*)*)'; if Exec (AText) then REPEAT if Match [3] = '812' then APhones.Add (Match [4]); UNTIL not ExecNext; finally Free; end; end; procedure testPerlRegex; var Rex: TPerlRegex; begin try Rex:= TPerlRegex.Create; try Rex.Subject:= 'This is a teetest of TPerlRegex'; Rex.RegEx:= '\bteetest\b'; //Rex.Regex := // '(?i)href="HTTP://(www\.)?([_a-z\d\-]+(\.[_a-z\d\-]+)?)/?"'; if Rex.Match then WriteLn('Found match: ' + Rex.MatchedText) else WriteLn('Can''t be! No match found!'); //ReadLn; finally Rex.Free; end; except //on E: Exception do Writeln(ExceptiontoString(ExceptionType, ExceptionParam)); end; end; //The RegularExpressions unit defines TRegEx and TMatch as object records. // SynRegExpr unit RegEx Snippet procedure testTRegExprFloat; begin with TRegExpr.Create do try Expression:= REGEXPRESSION1; //'[+-]?([0-9]*[.])?[0-9]+'; if Exec ('3.14159') then writeln(Match[0]+' Match found'); finally Free; end; end; procedure testPerlRegexFloat; var Rex: TPerlRegex; begin try Rex:= TPerlRegex.Create; try Rex.Subject:= '3.141592'; Rex.RegEx:= REGEXPRESSION1; //'[+-]?([0-9]*[.])?[0-9]+'; if Rex.Match then WriteLn('Found float match: ' + Rex.MatchedText) else WriteLn('No Float match found!'); //ReadLn; finally Rex.Free; end; except //on E: Exception do Writeln(ExceptiontoString(ExceptionType, ExceptionParam)); end; end; //C:\Program Files (x86)\maxbox3\source\uPSI_StRegEx.pas //regex of Systools4 add TStNodeHeap = class procedure testTRegExprSystoolsFloat; var REPosition : TMatchPosition; begin with TSTRegEx.Create(self) do try savestrtofile('3.1415926535',Exepath+'pifile.txt') inputfile:= Exepath+'pifile.txt' ; //REGEXPRESSION1; //RegisterMethod('Function CheckString( const S : AnsiString; var REPosition : TMatchPosition): Boolean'); //OutputOptions matchpattern.clear; //matchpattern.add('<[^>]*>'); matchpattern.add(REGEXPRESSION1); CheckString('3.14159', REPosition); outputfile:= exepath+'pifile2.txt' //Expression:= REGEXPRESSION1; //'[+-]?([0-9]*[.])?[0-9]+'; if Execute then writeln(' Systools Float Match found'); finally Free; end; end; procedure STEXpressionPattern; begin with TStExpression.create(self) do begin AddConstant('MPI',3.14159); srlist:= TStringlist.create; GetIdentList(srlist); for it:= 0 to srlist.count-1 do writeln(srlist.strings[it]); srlist.Free; Expression:= '2^10 * ln(8 +MPI)'; writeln(floattoStr(AnalyzeExpression)) free; end; end; function CheckCoefficientBoxesValidInput(InputtedTerm : TEdit) : boolean; var RegularExpressionq : TRegExp; //Match : TMatch; begin {RegularExpressionq:= TRegExp.Create('[-+]?[0-9]*\.?[0-9]+'); Match := RegularExpression.Match(InputtedTerm.Text); if Match.Success then begin ShowMessage('Success.'); end; } end; procedure TgbGoogleParseNaturalListings(ResponseHTML, ResultHostName: string); var Regex: TPerlRegex; begin Regex:= TPerlRegex.Create; RegEx.Regex := '(?i)href="HTTP://(www\.)?([_a-z\d\-]+(\.[_a-z\d\-]+)?)/?"'; RegEx.Subject := ResponseHTML; regex.study if RegEx.Match then repeat //Kind := 1; //ResultHostName := LowerCase(RegEx.SubExpressions[2]); //AddSearchTermAreaResult; until not RegEx.MatchAgain; //http://docwiki.embarcadero.com/CodeExamples/Rio/en/TPerlRegExStudy_(Delphi) with RegEx do begin RegEx := 'Edit1.Text'; Study; Subject := Memo2.Lines.Text; Replacement := 'Edit2.Text'; ReplaceAll; Memo2.Lines.Text := Subject; end; //http://docwiki.embarcadero.com/CodeExamples/Rio/en/TPerlRegExGroups_(Delphi) with regex do begin RegEx := '(Joe|Jack|William|Avarell) (Dalton)? did it'; Subject := 'The sheriff told us that Joe Dalton did it.'; if Match then if GroupCount = 2 then ShowMessage('Wanted: ' + Groups[1] + ' ' + Groups[2]) else ShowMessage('Wanted: ' + Groups[1] + ' (probably Dalton)'); end; end; //http://docwiki.embarcadero.com/CodeExamples/Rio/en/TPerlRegExMatchAgain_(Delphi) procedure TForm1Button1Click(Sender: TObject); var I: Integer; PerlRegEx1: TPerlRegex; begin PerlRegEx1:= TPerlRegex.Create; with PerlRegEx1 do begin RegEx := UTF8toString('Edit1.Text'); Subject := UTF8toString('Edit2.Text'); if Match then begin I := 1; while MatchAgain do I := I + 1; ShowMessage(Format('The regular expression matched the subject string %d times.', [I])); end else ShowMessage('regular expression did''t match the subject string at all.'); end; end; procedure TForm1Button1Click4(Sender: TObject); var PerlRegEx1: TPerlRegex; begin PerlRegEx1:= TPerlRegex.Create; with PerlRegEx1 do begin //State := [preNotBOL]; Subject := 'I like to hang out at maXbox bar'; RegEx := '([A-Za-z]+) bar'; Replacement := '\1 is the name of the bar I like'; if Match then ShowMessage(PerlRegEx1.ComputeReplacement); end; end; procedure TForm1Button1Click5(Sender: TObject); var S : UTF8String; var PerlRegEx1: TPerlRegex; begin // The first example generates an ERegularExpressionsError with message // 'Successful match required.' because the Groups have been cleared when the // Subject is set to empty: //Exception: REQUIRE: There must be a successful match first PerlRegEx1:= TPerlRegex.Create; S := 'The sheriff told us that Joe did it.'; with PerlRegEx1 do begin RegEx := '(Joe|Jack|William|Avarell) did it'; Subject := S; if Match then begin Subject := ''; // Save memory if GroupCount = 1 then S := 'Wanted: ' + Groups[1] else S := 'Nobody wanted'; ShowMessage(S); end; end; end; //http://docwiki.embarcadero.com/CodeExamples/Rio/en/TPerlRegExStoreGroups_(Delphi) procedure TForm1Button2Click(Sender: TObject); var S : UTF8String; PerlRegEx1: TPerlRegex; begin // The second example displays "Wanted: Joe". PerlRegEx1:= TPerlRegex.Create; S := 'The sheriff told us that Joe did it.'; with PerlRegEx1 do begin RegEx := '(Joe|Jack|William|Avarell) did it'; Subject := S; if Match then begin StoreGroups; Subject := ''; // Save memory if GroupCount = 1 then S := 'Wanted: ' + Groups[1] else S := 'Nobody wanted'; ShowMessage(S); end; end; PerlRegEx1.Free; end; procedure CheckRegex(const Input: string); begin //Writeln(Input, ' ', TRegEx.IsMatch(Input, '^\s*[-+]?[0-9]*\.?[0-9]+\s*$')); Writeln(Input+' '+booleanToString(ExecRegExpr(REGEXPRESSION3,Input))) end; function getMatchString(arex, atext: string): string; var RegEx: TPerlRegEx; WordCount: Integer; begin RegEx:= TPerlRegEx.Create; try regEx.Subject:= atext; RegEx.regex:= arex; WordCount := 0; { The Match function performs search and returns...} if RegEx.Match then { If there was a match, enter a loop to extract, count and write all matches and captured substrings. } repeat Inc(WordCount); { Write the complete matched string. } WriteLn('match '+itoa(WordCount)+ ' - '+ RegEx.MatchedText+ { Write the matched substrings. } +' - last debug: '+ RegEx.groups[1]); //RegEx.SubStr(1), result:= result+RegEx.MatchedText; until NOT RegEx.matchagain; //MatchNext < 0; //RegEx.CompileOptions:= RegEx.CompileOptions + [coNoAutoCapture]; //RegEx.Options:= RegEx.Options - [preNoAutoCapture]; RegEx.Options:= RegEx.Options- [preUnGreedy]; finally RegEx.Free; end; WriteLn('Done REX - Press Enter to exit'); end; function IsValidNumber4(s: String): Boolean; var parts: TStringDynArray; begin //parts := SplitStringEx(s, '.'); //function SplitStr(sInput:string; Delimiter:string): TStringArray; parts := SplitStr(s, '.'); case Length(parts) of 1: result := StrToIntDef(s, -1) <> -1; 2: result := (StrToIntDef(parts[0], -1) <> -1) and (StrToIntDef(parts[1], -1) <> -1); else result := FALSE; end; end; var rndpic: integer; iserv: ISWBemServices; iset: ISWbemObjectSet; wmi_dom: string; ENum: IEnumVariant; vObj: OleVariant; Begin //@main //