Recent

Author Topic: Extract numbers from a string  (Read 15605 times)

Sennar

  • New member
  • *
  • Posts: 8
Extract numbers from a string
« on: November 19, 2017, 08:51:57 pm »
Hi,
I am sorry for any mistakes I make, but english is not my first language.

I want to create a program where I enter a string (a text for example) and it gives back any numbers included in it. I have no idea how to do so with Lazarus, so I am looking forward to get some ideas here.

Thanks in advance! ;)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Extract numbers from a string
« Reply #1 on: November 19, 2017, 08:55:12 pm »
"numbers"
Do you want to identify only positive integers in your string, or will your string include negative and floating point numbers?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Extract numbers from a string
« Reply #2 on: November 19, 2017, 08:55:27 pm »
Hi,
I am sorry for any mistakes I make, but english is not my first language.

I want to create a program where I enter a string (a text for example) and it gives back any numbers included in it. I have no idea how to do so with Lazarus, so I am looking forward to get some ideas here.

Thanks in advance! ;)

Depending of the format of the string you provide.

If is a sequence of numbers: is space separated, comma separated, or even a single number.

Hope you're not a spammer also  ::)

Sennar

  • New member
  • *
  • Posts: 8
Re: Extract numbers from a string
« Reply #3 on: November 19, 2017, 09:04:52 pm »
No I am not a spammer, but thanks for the fast reply. The text shouldnt consist just out of numbers, but I want to extract all positive integers out of it. They are seperated by a space.

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Extract numbers from a string
« Reply #4 on: November 19, 2017, 09:23:10 pm »
Give us a sample of the string, and show us what you have tried before.

(Homework aasignment?)

Bart

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Extract numbers from a string
« Reply #5 on: November 19, 2017, 09:36:06 pm »
Use Regular Expressions. Here is an example applied on abc123def456 789,10.123:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, RegExpr
  10.   { you can add units after this };
  11.  
  12. var
  13.   re: TRegExpr;
  14. begin
  15.   re := TRegExpr.Create;
  16.   try
  17.     re.Expression:='\d+';
  18.     if re.Exec('abc123def456 789,10.123') then
  19.       repeat
  20.         WriteLn(re.Match[0]);
  21.       until not re.ExecNext;
  22.   finally
  23.     re.Free;
  24.   end;
  25.   WriteLn('Done!');
  26.   ReadLn;
  27. end.

The result is:
Quote
123
456
789
10
123
Done!

Use StrToInt from unit SysUtils to convert each of these strings to integer.

Edit:
or if your numerical string is assigned to a string variable S:
Code: Pascal  [Select][+][-]
  1. i := S.ToInteger;
« Last Edit: November 19, 2017, 09:39:01 pm by engkin »

Sennar

  • New member
  • *
  • Posts: 8
Re: Extract numbers from a string
« Reply #6 on: November 19, 2017, 09:39:54 pm »
Its kind of a homework assignment. I have, for example, the following list of cities with inhabitants. If I insert this into an Edit field, it should give the integers back. I have no idea how to do this, so I started this subject.

Shanghai 24,256,800 Municipality 6,340.5 3,826     China
Beijing 21,516,000 Municipality 16,410.54 1,311     China
Delhi 16,787,941 Union territory 1,484    11,313     India
Lagos 16,060,303 Metropolitan City 1,171.2 813,712     Nigeria
Tianjin 15,200,000 Municipality 11,760 1,293     China
Karachi  14,910,352 Metropolitan Corporation 3,527    4,572     Pakistan
Istanbul 14,160,467 Metropolitan Municipality 5,461 2,593     Turkey

Sennar

  • New member
  • *
  • Posts: 8
Re: Extract numbers from a string
« Reply #7 on: November 19, 2017, 09:41:51 pm »
Thank you, your code is very helpful! I will try to work with this.
Thanky you very much! ;)

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Extract numbers from a string
« Reply #8 on: November 19, 2017, 10:27:42 pm »
Yes, your teacher is gonna love regexpressions...

Before starting to code, analyse the problem and write out how you would solve it.
How do you know some part of the string (a word) is (maybe) a number?
How do you know (from your example text) a new word is starting?
Can you break down the string into seperate words? Then analize each of them?

Certainly you can do this in your head.
Now try to make that an algorithm.
Use "pseudo code" like:

Code: [Select]
repeat
  get first/next word
  if a number then handle it,
  otherwise dismiss
until end of line

Bart

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Extract numbers from a string
« Reply #9 on: November 20, 2017, 08:42:27 am »
It looks like a file with fixed 'colums'. Look at Tstringlist.
But I think your teacher wants you to use assignfile and array's.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Sennar

  • New member
  • *
  • Posts: 8
Re: Extract numbers from a string
« Reply #10 on: November 26, 2017, 02:46:39 pm »
Hi, Its me again. I tried out regular expressions and did what everyone else said. The next step I wanted to take is to write an application. I want to insert a text, which also includes integers and real numbers, into an TEdit field, press a button and get the numbers out of it, which should be delivered to another Edit field. I also want to include real numbers, so I guess two numbers are just seperated by a space, but not by a point or a comma. Does anyone have an idea how I could do this?


KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: Extract numbers from a string
« Reply #11 on: November 26, 2017, 03:58:45 pm »
what about simply

Code: Pascal  [Select][+][-]
  1. program HelloWorld;
  2.  
  3. function extractnumbers(line: string): string;
  4. const
  5.   n = ['0'..'9'];
  6. var
  7.   i: integer;
  8. begin
  9.   i := 1;
  10.   extractnumbers :='';
  11.   while  i < length(line) do
  12.   begin
  13.     if line[i] in n then extractnumbers := extractnumbers + line[i];
  14.     inc(i);
  15.   end;
  16. end;  
  17.  
  18. begin
  19.     writeln(extractnumbers('Hello, world123!'));
  20. end.
  21.  
outputs 123

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Extract numbers from a string
« Reply #12 on: November 26, 2017, 04:28:51 pm »
what about simply

Code: Pascal  [Select][+][-]
  1. program HelloWorld;
  2.  
  3. function extractnumbers(line: string): string;
  4. const
  5.   n = ['0'..'9'];
  6. var
  7.   i: integer;
  8. begin
  9.   i := 1;
  10.   extractnumbers :='';
  11.   while  i < length(line) do
  12.   begin
  13.     if line[i] in n then extractnumbers := extractnumbers + line[i];
  14.     inc(i);
  15.   end;
  16. end;  
  17.  
  18. begin
  19.     writeln(extractnumbers('Hello, world123!'));
  20. end.
  21.  
outputs 123

... but it wouldn't take account of reals, signed numbers or multiple numbers in one string. To handle the first two, a simple change to the constant 'n' by adding  '-', '+' and '.' would improve matters
Code: Pascal  [Select][+][-]
  1. const
  2.   n = ['0'..'9','-','+','.'];
  3.  

but to account for multiple numbers you need to handle spaces - assuming you accept a space as a delimiter.

Then again you may want to accept a comma as either a thousands or decimal delimiter.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Extract numbers from a string
« Reply #13 on: November 26, 2017, 04:43:34 pm »
Like I suggested before?
Break up the string in words, evaluate each word if it is a number or not.
Assume words are separated by (one or more) spaces.

If you know at forehand how much numbers there are, and even better if there is a distinct order in which integers and floats occur, that would make things much easier.

Bart

Sennar

  • New member
  • *
  • Posts: 8
Re: Extract numbers from a string
« Reply #14 on: November 26, 2017, 04:58:42 pm »
Yes, like you suggested before. I dont know how many numbers there will be in the Edit field and they also dont have a certain order. I have no clue how to transform this into lazarus, so suggestions would be welcome. Thanks in advance. (I mean, how do I break up a string from an Edit field into words for example? And how would you evaluate if a string is a number ( I mean, it should consist out of 1,2,3,4,5,6,7,8,9 and maybe "." and ",", but how would you write this in Lazarus?) I dont have much experience with it, so I have no idea how to transform the pseudo code into Lazarus)

 

TinyPortal © 2005-2018