官方的正则表达式组件 RegularExpressions (4) : 表达式选项

前端之家收集整理的这篇文章主要介绍了官方的正则表达式组件 RegularExpressions (4) : 表达式选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. TRegExOption = (
  2. roNone,//无
  3. roIgnoreCase,//不区分大小写
  4. roMultiLine,//多行模式; 可使 ^ 和 $ 匹配每个行首或行尾
  5. roExplicitCapture,//只捕获指定了名称或编号的子表达式
  6. roCompiled,//预编译表达式; 这在反复使用更有效率
  7. roSingleLine,//单行模式; 使 . 也可匹配换行符
  8. roIgnorePatternSpace //忽略注释和未经转义的空白
  9. );


  1. uses RegularExpressions;
  2.  
  3. {roIgnoreCase}
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. const
  6. pattern = '[A-Z]+\d+';
  7. txt = 'AAA1 bbb2 aa11 bb22 A111 B222 AAAA';
  8. var
  9. match: TMatch;
  10. begin
  11. Memo1.Clear;
  12. for match in TRegEx.Matches(txt,pattern,[roIgnoreCase]) do
  13. begin
  14. Memo1.Lines.Add(match.Value);
  15. end;
  16.  
  17. Memo1.Lines.Add('----------');
  18.  
  19. for match in TRegEx.Matches(txt,pattern) do
  20. begin
  21. Memo1.Lines.Add(match.Value);
  22. end;
  23. end;
  24. {*********************
  25. AAA1
  26. bbb2
  27. aa11
  28. bb22
  29. A111
  30. B222
  31. ----------
  32. AAA1
  33. A111
  34. B222
  35. **********************}
  36.  
  37. {roMultiLine}
  38. procedure TForm1.Button2Click(Sender: TObject);
  39. const
  40. txt = 'Delphi Delphi Delphi'#13#10'Delphi Delphi Delphi';
  41. var
  42. rStr: string;
  43. begin
  44. Memo1.Clear;
  45. {行首}
  46. rStr := TRegEx.Replace(txt,'^Delphi','......',[roMultiLine]);
  47. Memo1.Lines.Add(rStr);
  48. Memo1.Lines.Add('--------------------');
  49. rStr := TRegEx.Replace(txt,'......');
  50. Memo1.Lines.Add(rStr);
  51.  
  52. Memo1.Lines.Add('--------------------');
  53. {行尾}
  54. rStr := TRegEx.Replace(txt,'Delphi$','......');
  55. Memo1.Lines.Add(rStr);
  56. end;
  57. {*********************
  58. ...... Delphi Delphi
  59. ...... Delphi Delphi
  60. --------------------
  61. ...... Delphi Delphi
  62. Delphi Delphi Delphi
  63. --------------------
  64. Delphi Delphi ......
  65. Delphi Delphi ......
  66. --------------------
  67. Delphi Delphi Delphi
  68. Delphi Delphi ......
  69. **********************}
  70.  
  71. {roExplicitCapture}
  72. procedure TForm1.Button3Click(Sender: TObject);
  73. const
  74. pattern1 = '([A-Z]+)(\d+)';
  75. pattern2 = '(?<name1>[A-Z]+)(\d+)';
  76. pattern3 = '([A-Z]+)(?<3>\d+)';
  77. txt = 'AA11 BB22';
  78. var
  79. match: TMatch;
  80. group: TGroup;
  81. begin
  82. Memo1.Clear;
  83.  
  84. for match in TRegEx.Matches(txt,pattern1,[roExplicitCapture]) do
  85. begin
  86. for group in match.Groups do
  87. begin
  88. Memo1.Lines.Add(group.Value);
  89. end;
  90. end;
  91. Memo1.Lines.Add('--------------------');
  92.  
  93. for match in TRegEx.Matches(txt,pattern1) do //此处把 pattern1 换做 pattern2 或 pattern3 均可
  94. begin
  95. for group in match.Groups do
  96. begin
  97. Memo1.Lines.Add(group.Value);
  98. end;
  99. end;
  100. Memo1.Lines.Add('--------------------');
  101. for match in TRegEx.Matches(txt,pattern2,[roExplicitCapture]) do
  102. begin
  103. for group in match.Groups do
  104. begin
  105. Memo1.Lines.Add(group.Value);
  106. end;
  107. end;
  108. Memo1.Lines.Add('--------------------');
  109. for match in TRegEx.Matches(txt,pattern3,[roExplicitCapture]) do
  110. begin
  111. for group in match.Groups do
  112. begin
  113. Memo1.Lines.Add(group.Value);
  114. end;
  115. end;
  116. end;
  117. {*********************
  118. AA11
  119. BB22
  120. --------------------
  121. AA11
  122. AA
  123. 11
  124. BB22
  125. BB
  126. 22
  127. --------------------
  128. AA11
  129. AA
  130. BB22
  131. BB
  132. --------------------
  133. AA11
  134. 11
  135. BB22
  136. 22
  137. **********************}
  138.  
  139. {roCompiled}
  140. procedure TForm1.Button4Click(Sender: TObject);
  141. var
  142. reg: TRegEx;
  143. begin
  144. reg := TRegEx.Create('\d+',[roCompiled]);
  145. Memo1.Text := reg.Replace('AA11 BB22','..'); //AA.. BB..
  146. end;
  147.  
  148. {roSingleLine}
  149. procedure TForm1.Button5Click(Sender: TObject);
  150. const
  151. pattern = '[A-Z]{1}.{1}';
  152. txt = 'A B C D'#13#10'A B C D'#13#10'A B C D';
  153. var
  154. rStr: string;
  155. begin
  156. Memo1.Clear;
  157. rStr := TRegEx.Replace(txt,'11',[roSingleLine]);
  158. Memo1.Lines.Add(rStr);
  159. Memo1.Lines.Add('--------------------');
  160. rStr := TRegEx.Replace(txt,'11');
  161. Memo1.Lines.Add(rStr);
  162. end;
  163. {*********************
  164. 11111111
  165. 11111111
  166. 111111D
  167. --------------------
  168. 111111D
  169. 111111D
  170. 111111D
  171. **********************}
  172.  
  173. {roIgnorePatternSpace}
  174. procedure TForm1.Button6Click(Sender: TObject);
  175. var
  176. rStr: string;
  177. begin
  178. Memo1.Clear;
  179. {忽略空白}
  180. rStr := TRegEx.Replace('ABC,A B C,AB C','AB C','...',[roIgnorePatternSpace]);
  181. Memo1.Lines.Add(rStr); //...,AB C
  182. rStr := TRegEx.Replace('ABC,'...');
  183. Memo1.Lines.Add(rStr); //ABC,...
  184. Memo1.Lines.Add('--------------------');
  185.  
  186. {使用注释}
  187. rStr := TRegEx.Replace('ABC123','ABC#*123',[roIgnorePatternSpace]);
  188. Memo1.Lines.Add(rStr); //...123
  189. rStr := TRegEx.Replace('ABC123','...');
  190. Memo1.Lines.Add(rStr); //...
  191. end;
  192. {*********************
  193. ...,AB C
  194. ABC,...
  195. --------------------
  196. ...123
  197. ...
  198. **********************}
  199.  
  200. {选项集合}
  201. procedure TForm1.Button7Click(Sender: TObject);
  202. const
  203. pattern = '^Delphi';
  204. txt = 'DELPHI DELPHI DELPHI'#13#10'delphi delphi delphi';
  205. begin
  206. Memo1.Text := TRegEx.Replace(txt,[roIgnoreCase,roMultiLine]);
  207. end;
  208. {*********************
  209. ...... DELPHI DELPHI
  210. ...... delphi delphi
  211. **********************}

猜你在找的正则表达式相关文章