AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

RegExpr to detect integers

Ein Thema von WojTec · begonnen am 16. Apr 2015 · letzter Beitrag vom 17. Apr 2015
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

RegExpr to detect integers

  Alt 16. Apr 2015, 11:24
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?
  Mit Zitat antworten Zitat
Der schöne Günther

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

AW: RegExpr to detect integers

  Alt 16. Apr 2015, 11:29
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.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#3

Re: RegExpr to detect integers

  Alt 16. Apr 2015, 11:48
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"
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.091 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
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: RegExpr to detect integers

  Alt 16. Apr 2015, 15:01
Oh, I understand. I simply thought that it is possible with regex. Thanks a lot!
  Mit Zitat antworten Zitat
SProske

Registriert seit: 16. Feb 2015
Ort: Halle/S.
116 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#6

AW: RegExpr to detect integers

  Alt 16. Apr 2015, 16:11
You could use the following RegEx for a positive check:

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

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#7

Re: RegExpr to detect integers

  Alt 17. Apr 2015, 11:14
Thanks

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'?
  Mit Zitat antworten Zitat
SProske

Registriert seit: 16. Feb 2015
Ort: Halle/S.
116 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#8

AW: RegExpr to detect integers

  Alt 17. Apr 2015, 11:26
(?<!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.
Sebastian

Geändert von SProske (17. Apr 2015 um 11:33 Uhr)
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 11:21 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