Recent

Author Topic: [SOLVED] Extracting substrings from strings  (Read 6221 times)

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
[SOLVED] Extracting substrings from strings
« on: December 07, 2019, 09:11:03 pm »
Dear ALL,

I have long strings of digits and symbols like this:

Code: Pascal  [Select][+][-]
  1. S := '1110-00(12)1000011000000020(01)02(12)0000020300100204020-0000002021122222010023013322101002000';

Each single digit or symbol counts as a single position. The digits enclosed between '(' and ')' should also count as a single position, therefore the above string should measure 85 positions (instead of 94, which is the length of the whole string).

I tried to play with the ExtractDelmited function from the StrUtils unit, but strangely the statement below:

Code: Pascal  [Select][+][-]
  1. C := ExtractDelimited(8, S, ['(', ')']);

returns nothing where I would expect it to return '(12)'.

Could someone give me a hand?

Thanks in advance!

« Last Edit: December 08, 2019, 12:16:02 pm by maurobio »
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Extracting substrings from strings
« Reply #1 on: December 07, 2019, 09:19:23 pm »
The help files states the first parameter is a WORD index, not a character index
so try this.

ExtractDelimited(1, s, ['(',')']);

The only true wisdom is knowing you know nothing

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Extracting substrings from strings
« Reply #2 on: December 07, 2019, 09:31:59 pm »
@jamie,

This does not work. it returns '1110-00' (the beginning of the string, immediately before the first target substring).

Similarly, using

Code: Pascal  [Select][+][-]
  1. ExtractDelimited(9, S, ['(', ')'])

(where 9 is the index of the first '(') return the whole string after the last target string.

BTW, what the definition of ExtracDelimited (https://www.freepascal.org/docs-html/rtl/strutils/extractdelimited.html) says is:

Quote
Extract the N-th delimited part from a string.

Anyway, I am not even sure if this approach is the most adequate for this problem.

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Extracting substrings from strings
« Reply #3 on: December 07, 2019, 10:00:06 pm »
Hi!

Before reading for hours manuals I would do it the simple way
Code: Pascal  [Select][+][-]
  1. p,q : integer;
  2.  
  3. p := pos ('(',s);
  4. q := pos (')', s);
  5. Wanted := copy (s,p,q-p+1);
  6.  
  7.  
Winni

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Extracting substrings from strings
« Reply #4 on: December 07, 2019, 10:09:08 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.  S = '1110-00(12)1000011000000020(01)02(12)0000020300100204020-0000002021122222010023013322101002000';
  4. begin
  5.   Caption := Copy(S,Pos('(',S)+1,Pos(')',S)-Pos('(',S)-1);
  6.  
  7. end;
  8.                                                        
  9.  
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Extracting substrings from strings
« Reply #5 on: December 07, 2019, 10:13:03 pm »
I did not know that this was a competetion who writes the best C style code in Pascal.

Winni

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Extracting substrings from strings
« Reply #6 on: December 07, 2019, 10:17:35 pm »
Me nether, just timing I guess.

 But in the case that he is doing, he would be better to use split there by specifying the '(' only..
then on the resulting of that, one can use split again on the ')' after which, there should be a list of all the numbers between the (.)  :)
The only true wisdom is knowing you know nothing

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Extracting substrings from strings
« Reply #7 on: December 07, 2019, 10:23:19 pm »
@jamie,

OK, but this only gives me the first delimited substring. How do I get the others, till the end of the original string?

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Extracting substrings from strings
« Reply #8 on: December 07, 2019, 10:30:08 pm »
That is what you asked for...

The first one..
in any case you can use split on the string..
Code: Pascal  [Select][+][-]
  1. var A :Array of string;
  2. Begin
  3.   A := S.Split(['(',')']);
  4.   If Length(A) <> 0 Then
  5.   Caption := A[3];  
  6. End;          
  7.  

Every odd number gives you the contents of each (?)..

Every even number gives you the contents of what is before it.

So
A[0] is the first content before the first (12)
A[1] is the (12);
A[2] is the content before the next (..);
A[3] etc////

The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Extracting substrings from strings
« Reply #9 on: December 07, 2019, 10:38:11 pm »
Hi!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var s : string;
  3. st : TStringList;
  4. p,q : integer;
  5. begin
  6. s:= '1110-00(12)1000011000000020(01)02(12)0000020300100204020-0000002021122222010023013322101002000';
  7. st := TStringList.create;
  8. repeat
  9. p := pos ('(',s);
  10. q := pos (')',s);
  11. if (q > 0) and (p> 0) then
  12.     begin
  13.     st.add(copy (s,p,q-p+1)); { with brackets }
  14.     delete (s,1,q);
  15.     end;
  16. until (p=0) or (q=0);
  17. showMessage (st.text);
  18. st.free;
  19. end;                    

Winni
« Last Edit: December 07, 2019, 10:46:33 pm by winni »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Extracting substrings from strings
« Reply #10 on: December 07, 2019, 10:48:51 pm »
Yum, Code!
The only true wisdom is knowing you know nothing

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Extracting substrings from strings
« Reply #11 on: December 08, 2019, 12:43:09 am »
or

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.     S, Buff : string;
  4.     PC : pchar;
  5.     I : integer = 1;
  6.     Bracket : boolean = false;
  7. begin
  8.   s:= '1110-00(12)1000011000000020(01)02(12)0000020300100204020-0000002021122222010023013322101002000';
  9.   PC := pchar(S);
  10.   while I < length(s) do begin
  11.     if (PC+i)^ = ')' then begin
  12.         if Buff <> '' then writeln('found one ' + Buff);
  13.         Bracket := False;
  14.     end;
  15.     if (PC+i)^ = '(' then begin
  16.         Buff := '';
  17.         Bracket := True;
  18.     end else
  19.         if Bracket then Buff := Buff + (PC+i)^;
  20.     inc(i);
  21.   end;
  22. end;      
     

Bet winni will find that a bit too C like.  :P

Should, IMHO be a bit faster and could be tweaked a lot. Only important if there is a lot of data to process.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Extracting substrings from strings
« Reply #12 on: December 08, 2019, 01:12:48 am »
@dbannon

No, I don't fight about some nanoseconds.

When it was necessary because of slow machines (long time ago...) you should have looked into my code. move was my best friend. But today with 4 cores and 8 threads ....

And they are all lazy. Even if I hear radio all day ....

But where are your results????

Winni

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Extracting substrings from strings
« Reply #13 on: December 08, 2019, 01:41:58 am »
... and how does  all that code handle a string where the open and close parentheses are not balanced ? 
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 2314
Re: Extracting substrings from strings
« Reply #14 on: December 08, 2019, 01:53:08 am »
I would have expected  someone mentioning the Copy2Symb-Function...
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018