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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| {$APPTYPE CONSOLE}
{$ASSERTIONS ON}
uses
System.SysUtils, System.RegularExpressions, System.TypInfo;
type
TSortTypeInterne = (
stiInteger,
stiCurrency,
stiNumeric,
stiDate,
stiText
);
function DetectType(const S: string): TSortTypeInterne;
function ExInt: string;
begin
result := '([+-]\s?)?\d+';
end;
function ExNum: string;
begin
with FormatSettings do
result := Format
(
'([+-]\s?)?(\d{1,3}%s?)*\d{1,3}%s\d+',
[ThousandSeparator, DecimalSeparator]
);
end;
function ExCur: string;
begin
with FormatSettings do
begin
result := Format
(
'([+-]\s?)?(\d{1,3}%s?)*\d{1,3}%s\d{1,%d}',
[ThousandSeparator, DecimalSeparator, CurrencyDecimals]
);
if Pos(CurrencyString, Format('%m', [1.50])) = 1 then
result := Format('(%s\s?)?%s', [CurrencyString, result])
else
result := Format('%s(\s?%s)?', [result, CurrencyString]);
end;
end;
function ExDat: string;
begin
with FormatSettings do
result := Format
(
'(0?[1-9]|[12][0-9]|3[01])%s(0?[1-9]|1[012])%s\d{4}',
[DateSeparator, DateSeparator]
);
end;
begin
if TRegEx.IsMatch(S, '^' + ExInt + '$') then
result := stiInteger
else
if TRegEx.IsMatch(S, '^' + ExNum + '$') then
result := stiNumeric
else
if TRegEx.IsMatch(S, '^' + ExCur + '$') then
result := stiCurrency
else
if TRegEx.IsMatch(S, '^' + ExDat + '$') then
result := stiDate
else
result := stiText;
end;
procedure Test(const S: string);
var
v: TSortTypeInterne;
begin
v := DetectType(S);
WriteLn('"', S, '"');
WriteLn(' ', GetEnumName(TypeInfo(TSortTypeInterne), Ord(v)));
WriteLn;
end;
const
EXEMPLES: array[0..11] of string = (
'1',
'+1',
'+ 1',
'1,5',
'+1,5',
'+ 1,5',
'1,50',
'1,50 ',
'1,50 ',
'11'#160'111,50 ',
'11'#160'111'#160'111,50 ',
'04/03/2017'
);
var
i: integer;
begin
try
Assert(FormatSettings.CurrencyString = '');
Assert(Format('%m', [1.50]) = '1,50 ');
Assert(Ord(FormatSettings.ThousandSeparator) = 160);
for i := Low(EXEMPLES) to High(EXEMPLES) do
Test(EXEMPLES[i]);
except
on E: Exception do
WriteLn(E.ClassName, ': ', E.Message);
end;
ReadLn;
end. |
Partager