regexapidemo.pp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. uses SysUtils, System.RegularExpressionsCore;
  2. Const
  3. Rec1 = 'Name:"John" Surname:"Doe" Email:"[email protected]"';
  4. Rec2 = 'Name:"Jane" Surname:"Dolina" Email:"[email protected]"';
  5. procedure DumpMatch(aIndex : Integer; aRegex : TPerlRegex);
  6. var
  7. I : integer;
  8. begin
  9. Writeln('Match ',aIndex,':');
  10. Writeln(' offset: ',aRegex.MatchedOffset);
  11. Writeln(' text: ',aRegex.MatchedText);
  12. Writeln(' Group count: ',aRegex.GroupCount);
  13. For I:=1 to aRegex.GroupCount do
  14. Writeln(' [',IntToStr(I),'] : ',aRegex.Groups[I]);
  15. Writeln(' Named group count: ',aRegex.NameCount);
  16. For I:=0 to aRegex.GroupCount-1 do
  17. With aRegex do
  18. Writeln(' [',IntToStr(I),'] name: "',Names[I],'", value: "',NamedGroups[Names[i]],'"');
  19. end;
  20. var
  21. RegEx : TPerlRegex;
  22. aIndex : integer;
  23. begin
  24. Regex:=TPerlRegex.Create;
  25. Regex.Subject:=Rec1+#10+Rec2;
  26. Regex.RegEx:='Name:"(?<Name>[\w]+?)".*?Surname:"(?<Surname>[\w]+?)".*?Email:"(?<Email>\b[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b)"';
  27. if not Regex.Match then
  28. Writeln('No match found')
  29. else
  30. begin
  31. aIndex:=1;
  32. DumpMatch(aIndex,Regex);
  33. While Regex.MatchAgain do
  34. begin
  35. Inc(aIndex);
  36. DumpMatch(aIndex,Regex);
  37. end;
  38. end;
  39. end.