Recent

Author Topic: [SOLVED] Extracting GameID from a string  (Read 605 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Extracting GameID from a string
« on: January 07, 2024, 07:36:52 pm »
Maybe a simple answer but how do I attract a game ID from this url?
Code: [Select]
https://api.hockeydata.net/data/ih/GameDocument?apiKey=a0a1258810e90e77a2367f403c6635d1&referer=https://www.ijshockeynederland.nl/&timestamp=1661761801761&gameId=22791959-929c-430d-8b6e-08f919f0a276&lang=en&docType=gameReport
« Last Edit: January 09, 2024, 12:02:53 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Extracting GameID from a string
« Reply #1 on: January 07, 2024, 07:44:58 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   s: string;
  3.  
  4. begin
  5.   s := 'https://api.hockeydata.net/data/ih/GameDocument?apiKey=a0a1258810e90e77a2367f403c6635d1&referer=https://www.ijshockeynederland.nl/&timestamp=1661761801761&gameId=22791959-929c-430d-8b6e-08f919f0a276&lang=en&docType=gameReport';
  6.   s := copy(s, pos('&gameId=', s));
  7.   s := copy(s, pos('=', s)+1);
  8.   s := copy(s, 1, pos('&', s)-1);
  9.  
  10.   writeln('game id = ', s);
  11.  
  12.   readln;
  13. end.

Code: Pascal  [Select][+][-]
  1. uses RegExpr;
  2.  
  3. var
  4.   s: string;
  5.   r: TRegExpr;
  6.  
  7. begin
  8.   s := 'https://api.hockeydata.net/data/ih/GameDocument?apiKey=a0a1258810e90e77a2367f403c6635d1&referer=https://www.ijshockeynederland.nl/&timestamp=1661761801761&gameId=22791959-929c-430d-8b6e-08f919f0a276&lang=en&docType=gameReport';
  9.  
  10.   r := TRegExpr.Create('&gameId=(.*)&');
  11.   r.ModifierG := false;
  12.  
  13.   try
  14.     if r.Exec(s) then begin
  15.       writeln('game id = ', r.Match[1]);
  16.     end;
  17.   finally
  18.     r.Free;
  19.   end;
  20.  
  21.   readln;
  22. end.

RayoGlauco

  • Full Member
  • ***
  • Posts: 179
  • Beers: 1567
Re: Extracting GameID from a string
« Reply #2 on: January 07, 2024, 07:49:08 pm »
Maybe you can use CodePointPos.
First, search for the string 'gameId=' and then search the next parameter or the end of the url:
Note that if gameID can be the first parameter, it will not be preceded by '&' but by '?'

Code: Pascal  [Select][+][-]
  1. IDini:=CodePointPos('gameId=', URLstring, 1) + CodePointLength('gameId=');
  2. IDend:=CodePointPos('&', URLstring, IDini+1)-1;
  3. if IDend<IDini then  // '&' not found
  4.   IDend:=CodePointLength(URLstring);
  5.  

(incomplete and untested code)
« Last Edit: January 07, 2024, 07:58:23 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Extracting GameID from a string
« Reply #3 on: January 08, 2024, 05:10:22 pm »
can also be done like this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   uriparser;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   url: String = 'https://api.hockeydata.net/data/ih/GameDocument?apiKey=a0a1258810e90e77a2367f403c6635d1&referer=https://www.ijshockeynederland.nl/&timestamp=1661761801761&gameId=22791959-929c-430d-8b6e-08f919f0a276&lang=en&docType=gameReport';
  7.   uri: TURI;
  8.   sl: TStringList;
  9. begin
  10.   uri := ParseURI(url);
  11.   sl := TStringList.Create;
  12.   sl.Delimiter := '&';
  13.   sl.DelimitedText := uri.Params;
  14.   ShowMessage(sl.Values['gameId']);
  15.   sl.Free;
  16. end;    
  17.  
  18.  
Best regards / Pozdrawiam
paweld

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Extracting GameID from a string
« Reply #4 on: January 09, 2024, 12:02:38 am »
Thanks everyone
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018