wasmregexpdemo.pp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. program wasmregexpdemo;
  2. uses sysutils, wasm.regexp.shared, wasm.regexp.api, wasm.regexp.objects;
  3. Const
  4. SRegex = 'quick\s(?<color>brown).+?(jumps)';
  5. STest = 'The Quick Brown Fox Jumps Over The Lazy Dog';
  6. SFlags = 'dgi';
  7. Var
  8. Regex : TWasmRegExp;
  9. Res : TRegExpResult;
  10. I : Integer;
  11. M : TRegExpMatch;
  12. G : TRegExpGroup;
  13. S : String;
  14. begin
  15. Writeln('Regular expression: ',SRegex);
  16. Writeln('Flags: ',SFlags);
  17. Regex:=TWasmRegExp.Create(SRegex,SFlags);
  18. Writeln('Test string: ',STest);
  19. Res:=Regex.Exec(STest);
  20. if Res.Index=0 then
  21. Writeln('No match')
  22. else
  23. With Res do
  24. begin
  25. Writeln('Match at : ',Index);
  26. I:=0;
  27. For M in Matches do
  28. begin
  29. S:=Format('(%d) : "%s"',[I,M.Value]);
  30. if (rfIndices in Regex.Flags) then
  31. S:=S+Format(' [From pos %d to %d]',[M.StartIndex,M.StopIndex]);
  32. Writeln(S);
  33. Inc(I);
  34. end;
  35. Writeln('Named groups : ',Length(Groups));
  36. For G in Groups do
  37. begin
  38. S:=Format('(%d): "%s": "%s"',[I,G.Name,G.Value]);
  39. if (rfIndices in Regex.Flags) then
  40. S:=S+Format(' [From pos %d to %d]',[G.StartIndex,G.StopIndex]);
  41. Writeln(S);
  42. Inc(I);
  43. end;
  44. end;
  45. end.