Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi RegExpr to detect integers (https://www.delphipraxis.net/184707-regexpr-detect-integers.html)

WojTec 16. Apr 2015 10:24

RegExpr to detect integers
 
Hi, I'm not sure is valid place for this question... But...

I want to extract numbers from string and Iactually did it with RegExpr. I have routines for floats and floats with unit (for data size, transfer speed and bandwidth). Floats look to be more complicated, but because there are more complicated it's easier to implement. For integers I have this:

Code:
[-+]?\d+

So, this can extract digits, not numbers, for example it returns "1.23" as "1" and "23", but should retun nothing (this is not integer, but float value). How it should be to recognize integers and do not split floats?

Der schöne Günther 16. Apr 2015 10:29

AW: RegExpr to detect integers
 
I've come to love
https://regex101.com/
and
http://www.regexr.com/

The latter lists
Code:
/(?:\d*\.)?\d+/ig
for parsing floats. You can find it at examples -> integer & decimal numbers.

WojTec 16. Apr 2015 10:48

Re: RegExpr to detect integers
 
Thanks men, but problem is how to get valid integers, not any numbers.

Integers: -9 1 2 3 100 255 etc.
Floats: -10.5 .5 7,17 10.5 etc.

Example from that site detects all numbers or split floats to 2 integers, but shouldn't. Form string "10rats + .36geese = 3.14cows" should get only "10" :(

Der schöne Günther 16. Apr 2015 11:36

AW: RegExpr to detect integers
 
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

WojTec 16. Apr 2015 14:01

Re: RegExpr to detect integers
 
Oh, I understand. I simply thought that it is possible with regex. Thanks a lot! :-D

SProske 16. Apr 2015 15:11

AW: RegExpr to detect integers
 
You could use the following RegEx for a positive check:

Code:
(?<!\.|\,|\d)([-+]?\d+)(?!\d*[\.|\,]\d*)
https://regex101.com/r/yU2vY2/1

WojTec 17. Apr 2015 10:14

Re: RegExpr to detect integers
 
Thanks :D

BTW: what mean ?<! ?!, because there are not literal symbols I think? Something like 'can't be behind string looking for' or 'con't start with'?

SProske 17. Apr 2015 10:26

AW: RegExpr to detect integers
 
(?<!XXX) is a negative lookbehind, it checks, that there is nothing fitting XXX directly before the captured string.
In your case it checks, that there is no ".", "," or any number.
(?!XXX) is a negative lookahead, it checks, that there is nothing fitting XXX directly behind the captured string.
In your case it checks, that there is not any amount (including 0) of numbers followed by "." or "," followed by any amount (including 0) of numbers

For further information you might check: http://www.regular-expressions.info/lookaround.html

E:
Just a little improvement on the regex:
Code:
(?<!\.|\,|\d)([-+]?\d+)(?!\d*[\.|\,]\d+)
The one before failed on: "My favourite number is 7."
This one captures a number, that is followed by a "." or "," but not a number again.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:38 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz