I'm no expert when it comes to regular expressions, but I suppose a "negative lookbehind" could do the trick. I tried, but the Delphi TRegex complained that the negative lookbehind was not of a fixed length (how could it?).
I guess it's way easier to just fetch all floats via regular expressions, then use code to see whether it fits into an integer or not.
Like this:
Delphi-Quellcode:
program Project26;
{$APPTYPE CONSOLE}
{$R *.res}
uses System.SysUtils, System.RegularExpressions,
Winapi.Windows;
function extractIntegers(
const fromMatches: TMatchCollection): TArray<Integer>;
var
match: TMatch;
newNumber: Integer;
begin
Result := TArray<Integer>.Create();
for match
in fromMatches
do
if Integer.TryParse(match.Value, newNumber)
then
Result := Result + [newNumber];
end;
procedure justRegexThings();
const
pattern = '
(?:\d*\.)?\d+';
content = '
10rats + .36geese = 3.14cows. Also, 14 or exactly 15.0 oranges, I''
m not sure.';
var
matches: TMatchCollection;
number: Integer;
begin
FormatSettings := TFormatSettings.Create(LOCALE_INVARIANT);
matches := TRegEx.Matches(content, pattern);
for number
in extractIntegers(matches)
do
WriteLn('
Found the number ', number);
end;
begin
try
justRegexThings();
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
readln;
end.
Outputs:
Code:
Found the number 10
Found the number 14