Einzelnen Beitrag anzeigen

SProske

Registriert seit: 16. Feb 2015
Ort: Halle/S.
116 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#10

AW: Regulärer Ausdruck für Hexzahlen

  Alt 30. Mär 2018, 16:51
Na das wird.. unschön - so in etwa:

Delphi-Quellcode:
program HexCheck;

uses
  System.RegularExpressions, System.SysUtils;

const
  HEX_STRINGS: array[0..6] of string = ('$AA', '00', '$AA00BB11CC22DD33', 'AA00BB11CC22DD33', 'A', 'XX1100', '$AA00BB11CC22DD33EE');
var
  M: TMatch;
  S: string;
  I: Integer;
begin
  for S in HEX_STRINGS do
  begin
    WriteLn(Format('Testing %s', [S]));
    I := 0;
    M := TRegex.Match(S, '(?:^\$(?=(?:[[:xdigit:]]{2}){1,8}$)|\G([[:xdigit:]]{2})(?=(?:[[:xdigit:]]{2}){0,7}$))');
    if M.Success then
    begin
      repeat
        WriteLn(Format('%d: %s', [I, M.Groups[0].Value]));
        Inc(I);
        M := M.NextMatch;
      until not M.Success;
    end else
      WriteLn(Format('No match for %s', [S]));
  end;
  ReadLn;
end.
Sebastian
  Mit Zitat antworten Zitat