Sign in Join
Wiki copytemporary

RegEx packages/zh CN: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: RegEx packages/zh CN

OldNewLine
1+Free Pascal 包含 RegExpr 包,该包包含多个用于正则表达式的“引擎”。其中主要的引擎是 TRegExpr,其他引擎已被弃用且不再开发。
2+
3+阅读关于正则表达式的 Wikipedia 页面以了解该主题:[POSIX 基本正则表达式](<http://en.wikipedia.org/wiki/Regular%20expression#POSIX%20Basic%20Regular%20Expressions>)。
4+
5+## Sorokin 的 TRegExpr
6+
7+这是最完整的实现,在 FPC 的 "packages/regexpr/src/regexpr.pas" 文件中可用。此包实现了 Perl 正则表达式的一个子集,并且该包提供的语法在 [这里有文档说明](<https://regex.sorokin.engineer/regular_expressions/>)。
8+
9+官方仓库:[GitHub](<https://github.com/andgineer/TRegExpr>)。开发在那里进行,FPC 包会稍晚一些跟随这个仓库。来自该仓库的版本在 FPC 中运行良好。
10+
11+### 2019/2020 年的改进
12+
13+在 2019 年之前,大约 15 年没有任何人提交补丁之后,[Alexey Torgashin](<https://github.com/Alexey-T/>) 和 [User:Alextp](</User:Alextp> "User:Alextp") 继续了 TRegExpr 的工作。这些更改已合并到 FPC Git 的“main”分支中。
14+
15+更改包括:
16+
17+ * 优化。一个主要的优化使得测试基准项目(在官方 GitHub 仓库中)在 1-2 个测试中速度提高了 5-10 倍。
18+ * 支持字符类内的元字符 \W \D \S。
19+ * 在字符类内添加了元字符 \h \H \v \V。
20+ * 支持命名组:(?P<name>regex),以及对命名组的反向引用:(?P=name)。
21+ * 支持非捕获组:(?:regex)。
22+ * 支持前瞻,正前瞻+负前瞻:foo(?=bar), foo(?!bar), (?<=foo)bar, (?<!foo)bar。
23+ * 支持原子组:(?>foo|bar)。
24+ * 支持占有量词:a++, a*+, a?+, a{n,m}+。
25+ * 支持 Unicode 类别:\pL \p{L} \p{Lu},带有否定 \P。
26+ * 支持控制转义序列:\cA ... \cZ。
27+ * 支持递归:(?R) 和 (?0)。
28+ * 支持子程序调用:(?1) ... (?80),以及对命名组的调用:(?P>name)。
29+ * 添加了 FastUnicodeData 定义,它使用额外的数组来加速 Unicode 模式下的 Unicode 函数。添加了 regexpr_unicodedata 单元。
30+ * 添加了向后搜索的可能性(ABackward 参数)。
31+ * 添加了在不前进到下一个位置的情况下在单个偏移量处测试正则表达式的可能性(ATryOnce 参数)。
32+ * 支持 NULL 字符,既在正则表达式中也在输入字符串中。
33+ * 支持大于 U+FFFF 的 Unicode,即表情符号。点、\W、\S、\D 等现在应该能够正确地为 1 个表情符号找到 2 个 WideChars。
34+ * 添加了 \z,更改了 \Z 的行为以与主要引擎一致。
35+
36+### 2023 年的改进
37+
38+在 2023 年,Martin Friebe(SynEdit 维护者)进行了许多很好的更改。
39+
40+这些更改也已合并到 FPC TRegExpr 分支中。
41+
42+ * 添加了 \K(从左截断匹配)。
43+ * 添加了 \G 以供 ExecNext 匹配上一个匹配结束的位置。
44+ * 添加了完整的正向前瞻支持。添加了完整的固定长度反向前瞻支持。添加了有限的可变长度反向前瞻支持。
45+ * 添加了 (?modifier:pattern) 支持。
46+ * 添加了“属性 AllowBraceWithoutMin”以允许 {,2} 而不是 {0,2}。
47+ * 添加了“属性 AllowLiteralBraceWithoutRange”以允许在没有后续范围时将“{”作为字面量匹配。
48+ * 优化,检测更多锚点:^ $ \G .*
49+ * 修复了“atomic”组。允许回溯整个组。
50+ * 移除了嵌套循环的“LoopStackMax = 10”限制。
51+
52+来自 Alexey Torgashin 的贡献:
53+
54+ * 添加了 \R 支持(任何换行符)。
55+
56+### 替换时更改大小写
57+
58+您可以更改找到的片段的大小写,在替换字段中使用修饰符(大小写更改在修饰符位置之后生效):
59+
60+ * \l - 将第一个字符转换为小写
61+ * \L - 将所有字符转换为小写
62+ * \u - 将第一个字符转换为大写
63+ * \U - 将所有字符转换为大写
64+
65+例如,如果找到了一个单词,使用替换字段 "\L$0" 将单词转换为小写(这里 $0 是组 0,即找到的文本)。
66+
67+此外,\n 将被替换为换行符(LF 或 CR LF,这取决于内部常量)。
68+
69+### 示例
70+
71+使用 TRegExpr 检查字符串中是否存在表达式非常简单,只需创建 TRegExpr 的一个实例,然后将您的正则表达式放入 TRegExpr.Expression 属性中,并使用 Exec 方法来验证该正则表达式是否有任何匹配项。如果表达式与传递给它的字符串匹配,Exec 将返回 true。
72+```
73+var
74+ RegexObj: TRegExpr;
75+ begin
76+ RegexObj := TRegExpr.Create;
77+ RegexObj.Expression := '.*login.*';
78+ if RegexObj.Exec('Please try to login here') then WriteLn('The login was found!');
79+ RegexObj.Free;
80+ end;
81+```
82+
83+子表达式匹配:
84+```
85+program Project1;
86+
87+ uses
88+ RegExpr;
89+
90+ var
91+ re: TRegExpr;
92+ begin
93+ re := TRegExpr.Create('hello (.*?)!');
94+ if re.Exec('hello world! hello pascal!') then
95+ begin
96+ WriteLn(re.Match[1]);
97+ while re.ExecNext do
98+ begin
99+ WriteLn(re.Match[1]);
100+ end;
101+ end;
102+ re.Free;
103+ end.
104+```
105+
106+输出:
107+```
108+world
109+ pascal
110+```
111+
112+## FLRE - 快速轻量级正则表达式
113+
114+FLRE(**F** ast **L** ight **R** egular **E** xpressions)是一个快速、安全且高效的正则表达式库,它使用Object Pascal(Delphi和Free Pascal)实现,但也可以从其他语言如C/C++等中使用。它能够处理Unicode和UTF-8字符串。
115+
116+它实现了许多最常见的Perl和POSIX特性,除了像[前向引用和嵌套反向引用](<https://www.regular-expressions.info/backref2.html>)等不规则表达式特性,这些在FLRE中不受支持,只支持真正的["反向引用"](<https://www.regular-expressions.info/backref.html>),这也是FLRE名称中“Light”一词的由来。它还查找最左最先匹配项,与Perl和PCRE相同,并能返回子匹配信息。但它还包含一个标志,用于实验性的POSIX风格的最左最长匹配行为模式。
117+
118+FLRE在LGPL v2.1静态链接异常下获得许可。
119+
120+[GitHub仓库](<https://github.com/BeRo1985/flre>)
121+
122+### 示例
123+
124+实现PHP的parse_url函数(基于[php.js parse_url](<http://locutus.io/php/url/parse_url/>)实现)
125+```
126+uses Classes, SysUtils, flre;
127+
128+ type
129+ RUrlParser = record
130+ private
131+ Fcomponents: array[1..13] of string;
132+ private
133+ function getComponent(aIndex: integer): string;
134+ procedure setComponent(aIndex: integer; const aValue: string);
135+ public
136+ function parse(const aUrl: UTF8String): boolean;
137+ public
138+ property scheme: string index 1 read getComponent write setComponent; // 例如 http
139+ property authority: string index 2 read getComponent;
140+ property userInfo: string index 3 read getComponent;
141+ property user: string index 4 read getComponent;
142+ property pass: string index 5 read getComponent;
143+ property host: string index 6 read getComponent;
144+ property port: string index 7 read getComponent;
145+ property path: string index 9 read getComponent;
146+ property directory: string index 10 read getComponent;
147+ property fileName: string index 11 read getComponent;
148+ property query: string index 12 read getComponent; // 问号(?)之后
149+ property fragment: string index 13 read getComponent; // 井号(#)之后
150+ end;
151+
152+ implementation
153+
154+ function RUrlParser.getComponent(aIndex: integer): string;
155+ begin
156+ Result := Fcomponents[aIndex];
157+ end;
158+
159+ procedure RUrlParser.setComponent(aIndex: integer; const aValue: string);
160+ begin
161+ Fcomponents[aIndex] := aValue;
162+ end;
163+
164+ function RUrlParser.parse(const aUrl: UTF8String): boolean;
165+ var
166+ i: integer;
167+ re: TFLRE;
168+ parts: TFLREMultiStrings;
169+ begin
170+ re := TFLRE.Create(
171+ '(?:([^:\\/\?#]+):)?'
172+ + '(?:\/\/()(?:(?:()(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?))?'
173+ + '()'
174+ + '(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)'
175+ , [rfUTF8]
176+ );
177+
178+ parts := nil;
179+ Result := re.UTF8ExtractAll(aUrl, parts);
180+ if Result then
181+ begin
182+ for i := 1 to Length(parts[0]) - 1 do
183+ begin
184+ setComponent(i, string(parts[0][i]));
185+ end;
186+ end;
187+
188+ // 释放正则表达式内存
189+ for i := 0 to Length(parts) - 1 do
190+ begin
191+ SetLength(parts[i], 0);
192+ end;
193+ parts := nil;
194+
195+ re.Free();
196+ end;
197+```
198+
199+## Joost的Regexpr
200+
201+Joost的Regexpr(单元为oldregexpr.pp和regex.pp)是一个非常基本的正则表达式单元,它处理大多数正则表达式,类似于GNU regexpr。
202+
203+当前的单元还远未完成,仍然缺少对[POSIX](<http://www.opengroup.org/onlinepubs/007908799/xbd/re.html>)或更复杂的语法如[Perl regex](<http://www.pcre.org/>)、[Java Regex](<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html>)、[Ruby Regex](<http://www.jpsoft.com/help/index.htm?regularexpressionsyntax.htm>)等的非常简单的语法支持。
204+
205+该单元目前包含4个函数:
206+
207+ * GenerateRegExprEngine – 编译正则表达式模式。
208+ * RegExprPos – 在给定字符串中查找模式。
209+ * DestroyRegExprEngine – 释放模式编译结果
210+ * RegExprEscapeStr – 转义正则表达式语言的保留语法,以便将其理解为字符串而不是正则表达式语法。
211+
212+还有一个测试:
213+
214+ * **testreg1** 测试程序演示了支持的正则表达式。
215+
216+## Florian的Regexpr
217+
218+这是最早的实现。它存在于packages/regexpr/src/old中,并且当前不被makefile编译,因此在发布的FPC/Lazarus版本中默认不可用预编译版本。
219+
220+---
221+*From the Free Pascal wiki, page "RegEx packages/zh CN", as the Internet Archive kept it on 2025-07-05, last changed on 2024-07-21. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history