AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

RegExpr to detect integers

Ein Thema von WojTec · begonnen am 16. Apr 2015 · letzter Beitrag vom 17. Apr 2015
 
Der schöne Günther

Registriert seit: 6. Mär 2013
6.191 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: RegExpr to detect integers

  Alt 16. Apr 2015, 12:36
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
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:25 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz