Search code examples
lazarus

Lazarus function to find 8 digit numbers in a string


I have e-mail subject lines and I want to find ticket references in them it could be the TT ref is like 12345678. One subject line (string) can have multiple 8 digit numbers!

I have been using the below code but it is merely stripping out the first 8 digits then doing a check if that is 8 char long:

function StripNumbers(const aString: string): string;
var
  C: char;
begin
  Result := '';
  for C in aString do
  begin
    if CharInSet(C, ['0'..'9']) then
    begin
      Result := Result + C;
    end;
  end;
end;   

Example:

my string variable is

subject := "yada yada XF12345678 blabla XF87654321 duh XF11.223344"

function GetTTRefs(subject) should result "12345678;87654321;"

Thank you for answers.


Solution

  • function GotTTRefs(Subject:string;Digits:Byte):string;
    var
      i:integer;
      TT:string;
    begin
       i:=1;
       while i <= Length(Subject)-Digits+1 do
       begin
         if Subject[i] in ['1'..'9'] then
            begin
              TT:=Copy(Subject,i,Digits);
              if (StrToQWordDef(TT, 0) <> 0) then
                  Result:=Result+TT+';';
            end;
         inc(i);
       end;
    end;