1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| {$APPTYPE CONSOLE}
uses
RegularExpressions, SysUtils;
const
SAMPLE: array[0..10] of string = (
'*ABORG-001360', // articles
'?CASTELO 1419-002370',
'*EX/MINOU1424-412380',
'*RUBAN-003001',
'1234578011230', // EAN
'3760162052329', // un vrai EAN dans ce cas le préfixe est 376
'1456441001360', // 2of5
// quelques "faux"
'AAAAAAA',
'*AAAAA-001',
'*AAAAAA001360',
'123121212'
);
const
PATTERN = '[*?](?<nom1>[^-]{1,15})-(?<nom2>\d{3})(?<nom3>\d{3})|(?<nom4>123\d{10})|(?<nom5>\d{13})';
var
g: TGroup;
e: TRegEx;
m: TMatch;
i: integer;
procedure ShowResult(aGroupName: string);
begin
WriteLn(Format(' %s = "%s"', [aGroupName, m.Groups[aGroupName].Value]));
end;
begin
e := TRegEx.Create(PATTERN);
for i := Low(SAMPLE) to High(SAMPLE) do
begin
WriteLn('SAMPLE[', i, ']');
m := e.Match(SAMPLE[i]);
if m.Success then
begin
if m.Groups['nom1'].Value <> '' then
begin
ShowResult('nom1');
ShowResult('nom2');
ShowResult('nom3');
end else
if m.Groups['nom4'].Value <> '' then
ShowResult('nom4')
else
ShowResult('nom5');
end;
end;
ReadLn;
end. |
Partager