Recent

Author Topic: [SOLVED]Using TRegExpr with a list, mixing a numbers list and a range of numbers  (Read 927 times)

devEric69

  • Hero Member
  • *****
  • Posts: 648
Hello,
I would like to check the entry of a user with class TRegExpr.

He should only be able to capture a mixture of 2 constraints:
1: a list of numbers between 1 and 15. For example: 1,5,7,11
2: a range of numbers, for example [4-15], which means 4..15, ie 4,5,6,7,8,9,10,11,12,13,14,15.

And he could mix the two constraints together. For example: 1,[3-13],15. Of course, knowing how to do 1°) and 2°), it'll be a simple OR like this: ((1)*|(2)*)

Any leads?
Regards.
« Last Edit: December 11, 2019, 11:22:59 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Using TRegExpr with a list, mixing a numbers list and a range of numbers
« Reply #1 on: December 11, 2019, 10:35:10 am »
1°) seems to me finally feasible, with '(\d{1,2})', and then by converting the regex's exactions into integer, and then checking if these integers are between the minimum allowed value and the maximum allowed value.

I always turn around how to make the range of numbers in 2°).
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Re: Using TRegExpr with a list, mixing a numbers list and a range of numbers
« Reply #2 on: December 11, 2019, 10:52:48 am »
1°) seems to me finally feasible, with '(\d{1,2})', and then by converting the regex's exactions into integer, and then checking if these integers are between the minimum allowed value and the maximum allowed value.

I always turn around how to make the range of numbers in 2°).

Hello! Here is my proposition. As you can see, I had the same idea as you.  :)

Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils, Classes, RegExpr;
  3.  
  4. function Check(const AStr: string): boolean;
  5. var
  6.   E, F: TRegExpr;
  7.   L: TStringList;
  8.   I, J, K: integer;
  9. begin
  10.   result := TRUE;
  11.   E := TRegExpr.Create('^\d{1,2}$');
  12.   F := TRegExpr.Create('^\[(\d{1,2})-(\d{1,2})\]$');
  13.   L := TStringList.Create;
  14.   L.DelimitedText := AStr;
  15.   try
  16.     for I := 0 to L.Count - 1 do
  17.     begin
  18.       if E.Exec(L[I]) then
  19.       begin
  20.         J := StrToIntDef(L[I], 0);
  21.         if (J >= 1) and (J <= 15) then
  22.         begin
  23.           WriteLn('OK: ', L[I], ' (J=', J, ')');
  24.           Continue;
  25.         end;
  26.       end;
  27.       if F.Exec(L[I]) then
  28.       begin
  29.         J := StrToIntDef(F.Match[1], 0);
  30.         K := StrToIntDef(F.Match[2], 0);
  31.         if (J >= 1) and (J <= 15) and (K >= 1) and (K <= 15) then
  32.         begin
  33.           WriteLn('OK: ', L[I], ' (J=', J, '; K=', K, ')');
  34.           Continue;
  35.         end;
  36.       end;
  37.       WriteLn('NOK: ', L[I]);
  38.       result := FALSE;
  39.       Break;
  40.     end;
  41.   finally
  42.     E.Free;
  43.     F.Free;
  44.     L.Free;
  45.   end;
  46. end;
  47.  
  48. var
  49.   S: string;
  50.  
  51. begin
  52.   S := '1,[3-13],15';
  53.   WriteLn(Check(S));
  54. end.
  55.  

Quote
OK: 1 (J=1)
OK: [3-13] (J=3; K=13)
OK: 15 (J=15)
TRUE
My projects are on Gitlab and on Codeberg.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Using TRegExpr with a list, mixing a numbers list and a range of numbers
« Reply #3 on: December 11, 2019, 11:11:31 am »
Hello @Roland57.

I had gone in another direction with a beginning of regex like this: '([0-1][0-9]|[0-9])\-([0-1][0-9]|[0-9])' based on https://www.regular-expressions.info/numericranges.html. It must also be able to do it, too.

But anyway, thank you, because your solution is fine with me :) .
« Last Edit: December 11, 2019, 11:13:35 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018