Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-10

How to use TRegExpr class (in synregexpr.pas)

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

FreeMan

Hello,
Lazarus Revision 43315
TRegExpr class in "/opt/lazarus/components/synedit/synregexpr.pas"

I need check IP address, my IP4OK function for this. If I sent variable result is false, but sent parameter like '127.0.0.1' result is true, what is wrong?

EDT_IP: TMaskEdit;
......
EDT_IP.EditMask:= '!999.999.999.999;1; ';
......
function IP4OK(pT: string): boolean;
var r  : TRegExpr;
     i  : integer;
     IP : String;
begin
   r := TRegExpr.Create;
   r.Expression := '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$';
   IP := pT;
   Result := r.Exec(pT);  //Line 1.
   Result := r.Exec(IP); //Line 2.
   IP := '127.0.0.1';
   Result := r.Exec(IP);  //Line 3.
   Result := r.Exec('127.0.0.1'); //Line 4.
   r.Free;
end;

firts test:
if IP4OK('127.0.0.1') then .... if I use this function like this, "// Line X. " all r.Exec function return True

second test via vairable:
run project and type "127.0.0.1" in EDT_IP then trigger if IP4OK(EDT_IP.Text) then .... if I use this function like this, "// Line 1. " r.Exec function return FALSE
"// Line 2. " r.Exec function return FALSE
"// Line 3. " r.Exec function return TRUE
"// Line 4. " r.Exec function return TRUE

Reply by email

Mark Morgan Lloyd
to FreeMan
FreeMan wrote 12 lines
Hello, Lazarus Revision 43315 TRegExpr class in "/opt/lazarus/components/synedit/synregexpr.pas" I need check IP address, my IP4OK function for this. If I sent variable result is false, but sent parameter like '127.0.0.1' result is true, what is wrong? EDT_IP: TMaskEdit; ...... EDT_IP.EditMask:= '!999.999.999.999;1; ';

I'd suggest checking /exactly/ what you're passing in the initial test case, i.e. make sure there's no leading spaces etc.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

FreeMan
to Mark Morgan Lloyd

Yes Mark, you rigth.

This code is working now, Problem was space char in EDT_IP.Text, it comes from format. This is normal, but at this time, why "Trim" function NOT DELETE space char ????? is this bug ?

var Q : String;
Const WhiteSpace = [#0..' '];
     function My_Trim(S : string): String;
     var I: Integer;
     begin
       Result := S;
       Try
        I := 1;
        while I<Length(S)do Begin
         if S[I]in WhiteSpace then Delete(S,I,1)
         else Inc(I,1);
        end;    // while
        Result := S;
       Except
       end;
     end;
//copied from /fpc271/rtl/objpas/sysutils/sysstr.inc  line 524
     function Trim_(const S: string): string;
     var Ofs, Len: integer;
     begin
       len := Length(S);
       while (Len>0) and (S[Len] in WhiteSpace) do
        dec(Len);
       Ofs := 1;
       while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do
        Inc(Ofs);
       result := Copy(S, Ofs, 1 + Len - Ofs);
     end ;
begin
   Q := Trim(EDT_IP.Text);// space not deleted
   Q := Trim_(EDT_IP.Text);// space not deleted
   Q := My_Trim(EDT_IP.Text); // space DELETED
   if IP4OK(Q)then begin
..............

24-10-2013 16:18 tarihinde, Mark Morgan Lloyd yazd?:

Show quoted text 17 lines
FreeMan wrote: > Hello, > Lazarus Revision 43315 > TRegExpr class in "/opt/lazarus/components/synedit/synregexpr.pas" > > I need check IP address, my IP4OK function for this. If I sent > variable result is false, but sent parameter like '127.0.0.1' result > is true, what is wrong? > > > EDT_IP: TMaskEdit; > ...... > EDT_IP.EditMask:= '!999.999.999.999;1; '; I'd suggest checking /exactly/ what you're passing in the initial test case, i.e. make sure there's no leading spaces etc.

Reply by email

Mark Morgan Lloyd
to FreeMan: Yes Mark, you rigth. This code is working now, Problem was space char in EDT_IP.Text, it…
FreeMan wrote 20 lines
Yes Mark, you rigth. This code is working now, Problem was space char in EDT_IP.Text, it comes from format. This is normal, but at this time, why "Trim" function NOT DELETE space char ????? is this bug ? var Q : String;Const WhiteSpace = [#0..' ']; function My_Trim(S : string): String; var I: Integer; begin Result := S; Try I := 1; while I<Length(S)do Begin if S[I]in WhiteSpace then Delete(S,I,1) else Inc(I,1); end; // while Result := S; Except end; end; //copied from /fpc271/rtl/objpas/sysutils/sysstr.inc line 524 function Trim_(const S: string): string; var Ofs, Len: integer; begin len := Length(S); while (Len>0) and (S[Len] in WhiteSpace) do dec(Len); Ofs := 1; while (Ofs<=Len) and (S[Ofs] in WhiteSpace) do Inc(Ofs); result := Copy(S, Ofs, 1 + Len - Ofs); end ;begin Q := Trim(EDT_IP.Text);// space not deleted Q := Trim_(EDT_IP.Text);// space not deleted Q := My_Trim(EDT_IP.Text); // space DELETED if IP4OK(Q)then begin..............

By the time that arrived here its formatting had been completely lost (as you can see above), but it doesn't look to bad in the archive http://lists.lazarus.freepascal.org/pipermail/lazarus/2013-October/084020.html so I'm not entirely sure what's going on.

I'd suggest using a supported compiler rather than the development one, i.e. 2.6.x not 2.7.x. I'd also suggest not copying around code fragments gratuitously, because there are plenty of ways that things can behave differently depending on the compiler mode.

/If/ it still fails with the supported compiler version, then give us a minimal compilable program that we can test, rather than just a fragment without context.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Frederic Da Vitoria
to Mark Morgan Lloyd

2013/10/24 Mark Morgan Lloyd

Show quoted text 25 lines
FreeMan wrote: > Yes Mark, you rigth. > This code is working now, Problem was space char in EDT_IP.Text, it comes > from format. This is normal, but at this time, why "Trim" function NOT > DELETE space char ????? is this bug ? > > var Q : String;Const WhiteSpace = [#0..' ']; > function My_Trim(S : string): String; var I: Integer; begin > Result := S; Try I := 1; while I<Length(S)do Begin > if S[I]in WhiteSpace then Delete(S,I,1) else Inc(I,1); > end; // while Result := S; Except end; end; > //copied from /fpc271/rtl/objpas/sysutils/**sysstr.inc line 524 > function Trim_(const S: string): string; var Ofs, Len: integer; > begin len := Length(S); while (Len>0) and (S[Len] in > WhiteSpace) do dec(Len); Ofs := 1; while (Ofs<=Len) and > (S[Ofs] in WhiteSpace) do Inc(Ofs); result := Copy(S, Ofs, 1 + > Len - Ofs); end ;begin Q := Trim(EDT_IP.Text);// space not deleted > Q := Trim_(EDT_IP.Text);// space not deleted Q := My_Trim(EDT_IP.Text); > // space DELETED if IP4OK(Q)then begin.............. > By the time that arrived here its formatting had been completely lost (as you can see above), but it doesn't look to bad in the archive http://lists.lazarus.freepascal.org/pipermail/lazarus/2013-October/084020.htmlso I'm not entirely sure what's going on

The formatting seemed correct in GMail.

Frederic Da Vitoria (davitof) Membre de l'April - ? promouvoir et d?fendre le logiciel libre ? - http://www.april.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20131025/f9bbfebf/attachment.html>

Reply by email

FreeMan
to Frederic Da Vitoria

This is version is penultimate version :)
'127.000. 0 .01 ' result is '127.0.0.1'
I tested this in:
Kubuntu 13.10 x64
fpc svn update 25845
lazarus svn update 43320

function IP4OK(pT: String): String;
var r  : TRegExpr;
     i,j: Integer;
     function My_Trim(S : String): String;
     Const myWhiteSpace = [#0..#32];
     var I: Integer;
     begin
      Result := '';
      Try
       I := 1;
       while I<=Length(S)do Begin
        if not(S[I]in MyWhiteSpace)then Result := Result + S[I];
        Inc(I,1);
       end;
      Except
       Result := '';
      end;
     end;
begin
   try
   Result := '';
   r := TRegExpr.Create;
   r.Expression := '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$';
   pT := My_Trim(pT);
   if r.Exec(pT) then begin
     for i := 1 to 4 do begin
       J := StrToInt(r.Match[i]);
       if J > 255 then begin
           Result := '';
           Break;
       end else begin
         if Result''then Result := Result + '.';
         Result := Result + IntToStr(J);//for delete space and zero on left
       end;
     end;
   end;
   finally
     r.Free;
   end;
end;

Reply by email

David Copeland
to FreeMan

I see that the starting IP can have imbedded spaces. Trim only removes leading and trailing space. I believe the DelSpace function will remove all space.

Dave.

FreeMan wrote 56 lines
This is version is penultimate version :) '127.000. 0 .01 ' result is '127.0.0.1' I tested this in: Kubuntu 13.10 x64 fpc svn update 25845 lazarus svn update 43320 function IP4OK(pT: String): String; var r : TRegExpr; i,j: Integer; function My_Trim(S : String): String; Const myWhiteSpace = [#0..#32]; var I: Integer; begin Result := ''; Try I := 1; while I<=Length(S)do Begin if not(S[I]in MyWhiteSpace)then Result := Result + S[I]; Inc(I,1); end; Except Result := ''; end; end; begin try Result := ''; r := TRegExpr.Create; r.Expression := '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$'; pT := My_Trim(pT); if r.Exec(pT) then begin for i := 1 to 4 do begin J := StrToInt(r.Match[i]); if J > 255 then begin Result := ''; Break; end else begin if Result''then Result := Result + '.'; Result := Result + IntToStr(J);//for delete space and zero on left end; end; end; finally r.Free; end; end; -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

FreeMan
to David Copeland

Thank you Dave,
I learned one more think too. I was write so long time ago similar "DelChars" function, now I delete it from my library. 'cos freepascal has it.
Delspace delete just #32. On my test I saw #0 (I don't now how and why)so I wroteguarantee code #0 to #32 will not useing chars so delete very small code.
strutils.pp has many string function, I been happy, thanks again Dave

25-10-2013 15:01 tarihinde, David Copeland yazd?:

Show quoted text 71 lines
I see that the starting IP can have imbedded spaces. Trim only removes leading and trailing space. I believe the DelSpace function will remove all space. Dave. On 10/25/2013 07:31 AM, FreeMan wrote: > This is version is penultimate version :) > '127.000. 0 .01 ' result is '127.0.0.1' > I tested this in: > Kubuntu 13.10 x64 > fpc svn update 25845 > lazarus svn update 43320 > > function IP4OK(pT: String): String; > var r : TRegExpr; > i,j: Integer; > > function My_Trim(S : String): String; > Const myWhiteSpace = [#0..#32]; > var I: Integer; > begin > Result := ''; > Try > I := 1; > while I<=Length(S)do Begin > if not(S[I]in MyWhiteSpace)then Result := Result + S[I]; > Inc(I,1); > end; > Except > Result := ''; > end; > end; > begin > try > Result := ''; > r := TRegExpr.Create; > r.Expression := '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$'; > pT := My_Trim(pT); > if r.Exec(pT) then begin > for i := 1 to 4 do begin > J := StrToInt(r.Match[i]); > if J > 255 then begin > Result := ''; > Break; > end else begin > if Result''then Result := Result + '.'; > Result := Result + IntToStr(J);//for delete space and zero on > left > end; > end; > end; > finally > r.Free; > end; > end; > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email