wasmregexpdemo.pp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly RegExp API - Demo program
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program wasmregexpdemo;
  12. uses sysutils, wasm.regexp.shared, wasm.regexp.api, wasm.regexp.objects;
  13. Const
  14. SRegex = 'quick\s(?<color>brown).+?(jumps)';
  15. STest = 'The Quick Brown Fox Jumps Over The Lazy Dog';
  16. SFlags = 'dgi';
  17. Var
  18. Regex : TWasmRegExp;
  19. Res : TRegExpResult;
  20. I : Integer;
  21. M : TRegExpMatch;
  22. G : TRegExpGroup;
  23. S : String;
  24. begin
  25. Writeln('Regular expression: ',SRegex);
  26. Writeln('Flags: ',SFlags);
  27. Regex:=TWasmRegExp.Create(SRegex,SFlags);
  28. Writeln('Test string: ',STest);
  29. Res:=Regex.Exec(STest);
  30. if Res.Index=0 then
  31. Writeln('No match')
  32. else
  33. With Res do
  34. begin
  35. Writeln('Match at : ',Index);
  36. I:=0;
  37. For M in Matches do
  38. begin
  39. S:=Format('(%d) : "%s"',[I,M.Value]);
  40. if (rfIndices in Regex.Flags) then
  41. S:=S+Format(' [From pos %d to %d]',[M.StartIndex,M.StopIndex]);
  42. Writeln(S);
  43. Inc(I);
  44. end;
  45. Writeln('Named groups : ',Length(Groups));
  46. For G in Groups do
  47. begin
  48. S:=Format('(%d): "%s": "%s"',[I,G.Name,G.Value]);
  49. if (rfIndices in Regex.Flags) then
  50. S:=S+Format(' [From pos %d to %d]',[G.StartIndex,G.StopIndex]);
  51. Writeln(S);
  52. Inc(I);
  53. end;
  54. end;
  55. end.