Author Topic: SScanF  (Read 861 times)

ginoo

  • Full Member
  • ***
  • Posts: 169
SScanF
« on: August 25, 2026, 05:21:09 pm »
With this string
554 "554 5.5.1 Error: no valid recipients"

I would like to obtain these results with sscanf

Code: Pascal  [Select][+][-]
  1. SScanf(esitoInvioMail, '%d "%s"', [@codiceErr, @Errore]);
  2.  

How can I configure the sscanf format to get these results?
codiceErr = 554
Errore =  554 5.5.1 Error: no valid recipients

sstvmaster

  • Sr. Member
  • ****
  • Posts: 313
Re: SScanF
« Reply #1 on: August 25, 2026, 05:54:10 pm »
greetings Maik

Windows, Linux
- Lazarus 4.8 (stable) + fpc 3.2.2 (stable)
- Lazarus 4.99 (trunk) + fpc 3.3.1 (main/trunk)

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #2 on: August 25, 2026, 05:54:46 pm »
@sstvmaster

Official documentation:
https://docs.freepascal.org/docs-html/rtl/sysutils/sscanf.html
Post crossed. That non-docu is (well) the wrong link.
You don't need Lazarus-ccr. It is standard.
And documented in the rtl documentation.
Check your links. >:(  Please. Always refer to the official documentation. O:-)

Am I the only one who reads the docs? <sigh>
« Last Edit: August 25, 2026, 06:29:27 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

paweld

  • Hero Member
  • *****
  • Posts: 1792
Re: SScanF
« Reply #3 on: August 26, 2026, 07:25:12 am »
@Thaddy - The link provided by @sstvmaster is also valid and official: https://forum.lazarus.freepascal.org/index.php/topic,72714.0.html
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #4 on: August 26, 2026, 08:22:14 am »
@paweld
I was referring to https://lazarus-ccr.sourceforge.io/docs/rtl/sysutils/sscanf.html
which may or may not be valid, but because it is a sourceforge link and lazarus-ccr I wonder if that it is current. The link to the official documentation is the proper, current, link, accessible through the documentation link on the forum:
https://www.freepascal.org/docs-html/current/rtl/sysutils/sscanf.html
or better
https://docs.freepascal.org/docs-html/rtl/sysutils/sscanf.html
These two are authoritative.
The sourceforge link probably showed up in a search.


More in general: https://www.freepascal.org/docs.var to which both resolve.

I checked: the sourceforge link is stale, not current. (What I suspected)
« Last Edit: August 26, 2026, 08:39:36 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: SScanF
« Reply #5 on: August 26, 2026, 08:34:35 am »
How can I configure the sscanf format to get these results?
You don't.
sscanf splits the sourcestring along blanks, and AFAIK you cannot influence this, except maybe parsing splitting by hand beforehand

Use Regex
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Sysutils, Regexpr;
  4. Const
  5.   s:String='554 "554 5.5.1 Error: no valid recipients"';
  6.   e:String='(\d+?)\s+(".+")';
  7. Var
  8.   CodiceErr:Integer;
  9.   Errore:String;
  10.   RegEx:TRegExpr;
  11.   b:Boolean;
  12. begin
  13.   RegEx:=TRegExpr.Create;
  14.   RegEx.ModifierG:=True;
  15.   RegEx.Expression:=e;
  16.   RegEx.InputString:=s;
  17.   b:=RegEx.Exec;
  18.   If b and (RegEx.SubExprMatchCount=2) Then
  19.     Begin
  20.       CodiceErr:=RegEx.Match[1].ToInteger;
  21.       Errore:=RegEx.Match[2];
  22.       writeln(CodiceErr);
  23.       Writeln(Errore);
  24.     end;
  25.   RegEx.Free;
  26.   Readln;
  27. end.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #6 on: August 26, 2026, 08:42:34 am »
SScanf behaves almost exactly like its C equivalent. So indeed, if it does not meet your requirements the above is a possible solution. You can also use the features from the string helpers from sysutils.
« Last Edit: August 26, 2026, 10:52:41 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: SScanF
« Reply #7 on: August 26, 2026, 09:02:16 am »
SSanf behaves almost exactly like its C equivalent. So indeed, if it does not meet your requirements the above is a possible solution. You can also use the features from the string helpers from sysutils.
Correct.
I took a look in the Pascal-Source for sscanf, and they check for a blank
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

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #8 on: August 26, 2026, 09:12:08 am »
simple example:
Code: Pascal  [Select][+][-]
  1. program stringsplit;
  2. {$mode objfpc}
  3. uses sysutils;
  4. var
  5.   s:string ='554 "554 5.5.1 Error: no valid recipients"';
  6.   a:TStringArray;
  7. begin
  8.   a :=s.split(['"']);
  9.   for s in a do writeln(s);
  10. end.
  11. {
  12. Outputs:
  13. 554
  14. 554 5.5.1 Error: no valid recipients
  15. }
Much easier than either sscanf or RexExpr.
Any "programmer" that knows only one programming language is not a programmer

cdbc

  • Hero Member
  • *****
  • Posts: 2974
    • http://www.cdbc.dk
Re: SScanF
« Reply #9 on: August 26, 2026, 09:17:47 am »
Hi
Hahaha, Thaddy beat me to it, but we had the same idea (k.i.s.s.):
Code: Pascal  [Select][+][-]
  1. procedure TTestForm.btnComplementClicked(Sender: TObject);
  2. var ls: string='554 "554 5.5.1 Error: no valid recipients"';
  3.   lsa: TAnsiStringArray;
  4. begin
  5.   lsa:= ls.Split([' '],'"');
  6.   TestEdit.Text:= 'CodiceErr: ' + lsa[0].Trim;
  7.   edtColOrg.Text:= 'Errore: ' + lsa[1].Trim.Replace('"','',[rfReplaceAll]);
  8. end;
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: SScanF
« Reply #10 on: August 26, 2026, 09:28:59 am »
i thought about that, too.
Caveat: It works as long as there is a double-quote, though i'm aware that in my case the Regex would fail, too.
But adjusting the Regex-Expression is simple, nevermind, that with regex you could even split/extract further information from the string.

Ah well.... as always in life: Many ways to skin a cat

adjusted RegEx-Pattern = (\d+?)\s+("{,1}.+"{,1})

Finds now both variants: w/o doublequotes

EDIT: Crap. I tested above adjusted pattern online, not in FPC/Lazarus
It chokes on the curly braces.... *sigh*
Workaround
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Sysutils, Regexpr;
  4. Const
  5.   s1:String='554 "554 5.5.1 Error: no valid recipients"';
  6.   s2:String='554 554 5.5.1 Error: no valid recipients';  //No Double quotes
  7.   e:Array[Boolean] Of String=('(\d+?)\s+(.+)','(\d+?)\s+"(.+)"');
  8. Var
  9.   CodiceErr:Integer;
  10.   Errore:String;
  11.   RegEx:TRegExpr;
  12.   b:Boolean;
  13.   c:Boolean;
  14. begin
  15.   RegEx:=TRegExpr.Create;
  16.   RegEx.ModifierG:=True;
  17.   c:=Pos('"',s1)>0;
  18.   RegEx.Expression:=e[c];
  19.   RegEx.InputString:=s1;
  20.   b:=RegEx.Exec;
  21.   If b and (RegEx.SubExprMatchCount=2) Then
  22.     Begin
  23.       CodiceErr:=RegEx.Match[1].ToInteger;
  24.       Errore:=RegEx.Match[2];
  25.       writeln('Source has doublequotes: ',CodiceErr);
  26.       Writeln('Source has doublequotes: ',Errore);
  27.     end;
  28.   c:=Pos('"',s2)>0;
  29.   RegEx.Expression:=e[c];
  30.   RegEx.InputString:=s2;
  31.   b:=RegEx.Exec;
  32.   If b and (RegEx.SubExprMatchCount=2) Then
  33.     Begin
  34.       CodiceErr:=RegEx.Match[1].ToInteger;
  35.       Errore:=RegEx.Match[2];
  36.       writeln('Source has no doublequotes: ',CodiceErr);
  37.       Writeln('Source has no doublequotes: ',Errore);
  38.     end;
  39.   RegEx.Free;
  40.   Readln;
  41. end.
Note: In this version i'm removing the doublequotes from the result
« Last Edit: August 26, 2026, 09:49:56 am by Zvoni »
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

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: SScanF
« Reply #11 on: August 26, 2026, 10:00:11 am »
Extracting more info
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Sysutils, Regexpr;
  4. Const
  5.   s1:String='554 "554 5.5.1 Error: no valid recipients"';
  6.   s2:String='554 554 5.5.1 Error: no valid recipients';  //No Double quotes
  7.   //e:Array[Boolean] Of String=('(\d+?)\s+(.+)','(\d+?)\s+"(.+)"');
  8.   d:Array[Boolean] Of String=('(\d+?)\s+(\d+?)\s+(\d+?.\d+?.\d+?)\s+(.+):\s+(.+)','(\d+?)\s+"(\d+?)\s+(\d+?.\d+?.\d+?)\s+(.+):\s+(.+)"');
  9. Var
  10.   CodiceErr:Integer;
  11.   //Errore:String;
  12.   MessageTitle:String;
  13.   MessageString:String;
  14.   ErrorNumber:Integer;
  15.   ErrorCode:String;
  16.   RegEx:TRegExpr;
  17.   b:Boolean;
  18.   c:Boolean;
  19. begin
  20.   RegEx:=TRegExpr.Create;
  21.   RegEx.ModifierG:=True;
  22.   c:=Pos('"',s1)>0;
  23.   RegEx.Expression:=d[c];
  24.   RegEx.InputString:=s1;
  25.   b:=RegEx.Exec;
  26.   If b and (RegEx.SubExprMatchCount=5) Then
  27.     Begin
  28.       CodiceErr:=RegEx.Match[1].ToInteger;
  29.       ErrorNumber:=RegEx.Match[2].ToInteger;
  30.       ErrorCode:=RegEx.Match[3];
  31.       MessageTitle:=RegEx.Match[4];
  32.       MessageString:=RegEx.Match[5];
  33.       writeln('Source has doublequotes: CodiceErr ->',CodiceErr);
  34.       Writeln('Source has doublequotes: ErrorNumber ->',ErrorNumber);
  35.       Writeln('Source has doublequotes: ErrorCode ->',ErrorCode);
  36.       Writeln('Source has doublequotes: MessageTitle ->',MessageTitle);
  37.       Writeln('Source has doublequotes: MessageString ->',MessageString);
  38.     end;
  39.   Writeln('------------------------------------------------');
  40.   c:=Pos('"',s2)>0;
  41.   RegEx.Expression:=d[c];
  42.   RegEx.InputString:=s2;
  43.   b:=RegEx.Exec;
  44.   If b and (RegEx.SubExprMatchCount=5) Then
  45.     Begin
  46.       CodiceErr:=RegEx.Match[1].ToInteger;
  47.       ErrorNumber:=RegEx.Match[2].ToInteger;
  48.       ErrorCode:=RegEx.Match[3];
  49.       MessageTitle:=RegEx.Match[4];
  50.       MessageString:=RegEx.Match[5];
  51.       writeln('Source has no doublequotes: CodiceErr ->',CodiceErr);
  52.       Writeln('Source has no doublequotes: ErrorNumber ->',ErrorNumber);
  53.       Writeln('Source has no doublequotes: ErrorCode ->',ErrorCode);
  54.       Writeln('Source has no doublequotes: MessageTitle ->',MessageTitle);
  55.       Writeln('Source has no doublequotes: MessageString ->',MessageString);
  56.     end;
  57.   RegEx.Free;
  58.   Readln;
  59. end.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #12 on: August 26, 2026, 10:03:10 am »
I supposed a fixed format: errorcode, errormessage.
Some remarks given  fixed format:
- Do NOT include space in the split options. Just one extra space would require TStringSplitOptions.ExcludeEmpty added.
- A slight implovement is to trim immediately:   for s in a do writeln(s.trim);
Both are just in case, given a fixed format. So the recommended demo would look like this:
Code: Pascal  [Select][+][-]
  1. program stringsplit;
  2. // given a fixed format: errorcode, double quoted errormessage
  3. {$mode objfpc}{$H+}
  4. uses sysutils;
  5. var
  6.   s:string ='554 "554 5.5.1 Error: no valid recipients"';
  7.   a:TStringArray;
  8. begin
  9.   a :=s.split(['"']);// do NOT include space!
  10.   for s in a do writeln(s.trim);// small change
  11. end.
That is given the fixed format, independent of spacing.
Otherwise I would recommend Zvoni's last option.

 
« Last Edit: August 26, 2026, 10:50:43 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 3595
Re: SScanF
« Reply #13 on: August 26, 2026, 11:29:01 am »
Improvement on Thaddy resp. different approach to StringSplit

Code: Pascal  [Select][+][-]
  1. program stringsplit;
  2. {$mode objfpc}{$H+}
  3. uses sysutils;
  4. var
  5.   s1:string ='554 "554 5.5.1 Error: no valid recipients"';
  6.   s2:string ='554 554 5.5.1 Error: no valid recipients';      //No doublequotes
  7.   a:TStringArray;
  8.   c:Integer;
  9.   r:String;
  10. begin
  11.   Writeln('Original Source: ',s1);
  12.   a :=s1.split([' ']);// split along space!
  13.   If High(a)>0 Then  //Could also check Length(a)>1
  14.     Begin
  15.       c:=a[0].ToInteger;
  16.       r:=String.Join(' ',a,1,Length(a)-1).Replace('"','',[rfReplaceAll]);
  17.     end;
  18.   Writeln(c);
  19.   Writeln(r);
  20.   Writeln('-------------------------------------');
  21.   Writeln('Original Source: ',s2);
  22.   a :=s2.split([' ']);// split along space!
  23.   If High(a)>0 Then  //Could also check Length(a)>1
  24.     Begin
  25.       c:=a[0].ToInteger;
  26.       r:=String.Join(' ',a,1,Length(a)-1).Replace('"','',[rfReplaceAll]);
  27.     end;
  28.   Writeln(c);
  29.   Writeln(r);
  30.   Readln;
  31. end.

As you can see: same code for both variants w/o Doublequotes
« Last Edit: August 26, 2026, 11:36:30 am by Zvoni »
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

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: SScanF
« Reply #14 on: August 26, 2026, 11:51:50 am »
I do not see that as an improvement. One space extra and the array length becomes three, not two. (unless TStringsplitoptions.Exclide empty is specified.
If the second string needs to be quoted, you can requote with s.QuotedString('"');
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018