Delphi 正则表达式类别Delphi 正则表达式类别Delphi 正则表达式类别Delphi 正则表达式类别
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
Delphi 正则表达式类别
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » Delphi 正则表达式类别

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
此网站上的更多信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

Delphi 正则表达式类别

Delphi XE 是第一个内置支持正则表达式的 Delphi 版本。在多数情况下,您会使用 RegularExpressions 单元。此单元定义了一组记录,用来仿真 .NET 架构中的正则表达式类别。就像在 .NET 中一样,它们让您只要一行代码就能使用正则表达式,而且不需要明确的内存管理。

RegularExpressions 单元在内部使用 RegularExpressionsCore 单元,而后者定义了 TPerlRegEx 类别。TPerlRegEx 是由本网站作者开发的开源 PCRE 函数库 的包装函数。因此,RegularExpressions 和 RegularExpressionsCore 单元都会使用 PCRE 正则表达式风格。

Delphi 的 RegularExpressions 单元

RegularExpressions 单元将 TRegEx、TMatch、TMatchCollection、TGroup 和 TGroupCollection 定义为记录,而不是类别。这表示您不需要调用 Create 和 Free 来配置和释放内存。

TRegEx 有个 Create 建构函数,如果你想要使用同一个正则表达式多次,你可以调用它。这样 TRegEx 就不会编译同一个正则表达式两次。如果你调用建构函数,你就可以调用任何不以正则表达式为参数的非静态方法。如果你没有调用建构函数,你只能调用以正则表达式为参数的静态(类别)方法。所有 TRegEx 方法都有静态和非静态的重载。你使用哪一个只取决于你是否想要使用同一个正则表达式调用 TRegEx 多次。

IsMatch 方法会接受一个字符串并回传 True 或 False,表示正则表达式是否符合(字符串的一部分)。

Match 方法会接受一个字符串并回传一个包含第一个符合项目的详细数据的 TMatch 记录。如果符合失败,它会回传一个 TMatch 记录,其中 Success 属性设为 nil。 Match() 的非静态重载会接受一个选用的起始位置和一个选用的长度参数,你可以使用它来只搜索输入字符串的一部分。

Matches 方法会接受一个字符串并回传一个 TMatchCollection 记录。这个记录的缺省 Item[] 属性会包含一个 TMatch,表示正则表达式在字符串中找到的每个符合项目。如果没有任何符合项目,回传的 TMatchCollection 记录的 Count 属性会是零。

使用 Replace 方法来搜索并取代字符串中的所有符合项目。你可以传递一个 TMatchEvaluator,它只是一个方法,它会接受一个称为 Match 的 TMatch 类型的参数并回传一个字符串。你的方法回传的字符串会用作一个文本替换字符串。如果你想要在使用 TMatchEvaluator 重载时替换字符串中的反向引用,请在回传字符串之前调用提供的 Match 参数上的 Result 方法。

使用 Split 方法来沿着其 regex 比对分割字符串。结果以动态字符串数组传回。与 .NET 中相同,在正则表达式中由 捕获组 比对的文本也包含在传回的数组中。如果您不喜欢这样,请从您的 regex 中移除所有 命名捕获组,并传递 roExplicitCapture 选项来禁用编号捕获组。 Split() 的非静态重载会采用一个选用的 Count 参数,以指出传回数组可能拥有的最大元素数。换句话说,字符串最多会分割 Count-1 次。捕获组比对不包含在计算中。因此,如果您的 regex 具有捕获组,传回的数组可能会有超过 Count 个元素。如果您传递 Count,您可以传递第二个选用参数,以指出字符串中开始分割的位置。传回数组的第一个元素会传回未分割的字符串起始位置之前的部分。

TMatch 记录提供几个有关比对的详细信息的属性。 Success 指出是否找到比对。如果这是 False,所有其他属性和方法都是无效的。 Value 传回比对的字符串。 Index 和 Length 指出输入字符串中的位置和比对的长度。 Groups 传回一个 TGroupCollection 记录,它在缺省的 Item[] 属性中为每个捕获组保存一个 TGroup 记录。您可以使用数字索引来 Item[] 编号捕获组,并使用字符串索引来 命名捕获组。

TMatch 也提供两个方法。 NextMatch 传回这个之后的正则表达式的下一个比对。如果您的 TMatch 是 TMatchCollection 的一部分,您不应该使用 NextMatch 来取得下一个比对,而是应该使用 TMatchCollection.Item[],以避免重复搜索。 TMatch.Result 采用一个参数,其中包含替换文本作为字符串。它传回如果您使用这个替换文本与 TRegEx.Replace,这个比对将会被替换的字符串。

TGroup 记录具有 Success、Value、Index 和 Length 属性,它们的工作方式就像 TMatch 的那些属性。

在 Delphi XE5 及之前版本中,TRegEx 始终会略过长度为零的比对。这已在 Delphi XE6 中修正。您可以通过修改 RegularExpressionsCore.pas 来移除 TPerlRegEx.Create 中的 State := [preNotEmpty] 行,在 XE5 及之前版本中进行相同的修正。这个变更也会影响直接使用 TPerlRegEx 而未设置 State 属性的代码。

较旧版本的 Delphi 的正则表达式类别

TPerlRegEx 在 Embarcadero 授权一份副本以包含在 Delphi XE 中之前就已经可以使用了。根据您的需求,您可以下载两个版本之一,以搭配 Delphi 2010 及更早版本使用。

  • 下载最新的基于类别的 TPerlRegEx
  • 下载较旧的基于组件的 TPerlRegEx

TPerlRegEx 的最新版本与 Delphi XE 中的 RegularExpressionsCore 单元完全兼容。对于以 Delphi 2010 或更早版本编写的新代码,强烈建议使用 TPerlRegEx 的最新版本。如果您稍后将代码移转到 Delphi XE,您所要做的就是将 PerlRegEx 取代为单元的 uses 子句中的 RegularExrpessionsCore。

较旧版本的 TPerlRegEx 是非视觉组件。这表示您可以将 TPerlRegEx 放到组件面板上,并将它拖放到表单上。最初的 TPerlRegEx 是在 Borland 的目标是让组件面板上的所有东西都有组件时开发的。

如果您想要从较旧版本的 TPerlRegEx 移转到最新的 TPerlRegEx,请从移除您可能已放置在表单或数据模块上的任何 TPerlRegEx 组件开始,并在运行阶段实例化对象。在运行阶段实例化时,您不再需要将拥有者组件传递给 Create() 构造函数。只要移除参数即可。

最初的 TPerlRegEx 中的一些属性和方法名称有点难以处理。这些名称已在最新的 TPerlRegEx 中重命名。基本上,在所有识别码中,SubExpression 已被 Group 取代,而 MatchedExpression 已被 Matched 取代。以下是已变更识别码的完整清单

旧识别码新识别码
StoreSubExpressionStoreGroups
NamedSubExpressionNamedGroup
MatchedExpressionMatchedText
MatchedExpressionLengthMatchedLength
MatchedExpressionOffsetMatchedOffset
SubExpressionCountGroupCount
SubExpressionsGroups
SubExpressionLengthsGroupLengths
SubExpressionOffsetsGroupOffsets

UTF-8 与 UTF-16

您需要留意的其中一件事是,您可以在这里下载的 TPerlRegEx 版本,以及包含在 Delphi XE、XE2 和 XE3 中的版本,都使用 UTF8String 属性,而所有 Offset 和 Length 属性都是这些 UTF-8 字符串的索引。这是因为当时 PCRE 只支持 UTF-8,而使用 UTF8String 可以避免重复转换。如果性能至关重要,您应该在这些 Delphi 版本中使用 TPerlRegEx,而不是 TRegEx。如果您的数据已经是 UTF-8,您可以将 UTF-8 直接传递给 TPerlRegEx。如果您的数据使用其他编码,您可以控制转换为 UTF-8 的时间,以避免重复转换相同的数据。

在 Delphi XE4 和 XE5 中,TPerlRegEx 具有 UnicodeString(UTF-16)属性,但仍然传回 UTF-8 的偏移量和长度。在 Delphi XE6 中,Offset 和 Length 属性已变更为 UTF-16 的偏移量和长度。这表示如果您的字符串包含非 ASCII 字符,使用 Offset 和 Length 属性的 XE3 或 XE6 兼容代码将无法与 XE4 和 XE5 兼容。从 Delphi XE4 到 Delphi 10 到 10.2,持续使用 UTF-8 版本的 PCRE,即使 PCRE 已经具有原生 UTF-16 支持。这与使用 UnicodeString 相结合,表示在 UTF-16 和 UTF-8 之间进行持续转换,这可能会大幅降低正则表达式的性能,特别是对于长的主旨字符串。

Delphi 10.3 和更新版本在 Windows 平台上使用 UTF-16 版本的 PCRE。TRegEx 和 TPerlRegEx 现在对所有内容都使用 UnicodeString,而不会转换为 UTF-8。从 Delphi XE4 或更新版本升级到 10.3 或更新版本,绝对会改善任何使用 TRegEx 或 TPerlRegEx 的代码性能。从 Delphi XE3 或更早版本升级会改善性能,除非您对所有内容都使用 UTF-8。

与 Delphi Prism 搭配使用 System.Text.RegularExpressions

Delphi Prism 是 Embarcadero 的 Delphi 语言变体,专门开发为针对 .NET 架构。Delphi Prism 存在于 Visual Studio IDE 中。它完全基于 .NET 架构。在 Delphi Prism 中,您可以简单地将 System.Text.RegularExpressions 命名空间添加到单元的 uses 子句。然后,您可以访问 .NET 正则表达式类别,例如 Regex、Match 和 Group。您可以使用 Delphi Prism,就像 C# 和 VB 开发人员可以使用它们一样。

与 Delphi for .NET 搭配使用 System.Text.RegularExpressions

Delphi 8、2005、2006 和 2007 包含一个 Delphi for .NET 编译器,用于开发 WinForms 和 VCL.NET 应用程序。尽管 Delphi for .NET 只支持 .NET 1.1 或 2.0(视您的 Delphi 版本而定),但您仍然可以使用 .NET 的完整正则表达式支持。您只需要将 System.Text.RegularExpressions 命名空间添加到单元的 uses 子句,就能够访问所有 .NET 正则表达式类别。

Delphi 正則表示式類別
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » Delphi 正則表示式類別

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
此網站上的更多資訊
簡介
正則表示式快速入門
正則表示式教學
替換字串教學
應用程式和語言
正則表示式範例
正則表示式參考
替換字串參考

Delphi 正則表示式類別

Delphi XE 是第一個內建支援正則表示式的 Delphi 版本。在多數情況下,您會使用 RegularExpressions 單元。此單元定義了一組記錄,用來模擬 .NET 架構中的正則表示式類別。就像在 .NET 中一樣,它們讓您只要一行程式碼就能使用正則表示式,而且不需要明確的記憶體管理。

RegularExpressions 單元在內部使用 RegularExpressionsCore 單元,而後者定義了 TPerlRegEx 類別。TPerlRegEx 是由本網站作者開發的開源 PCRE 函式庫 的包裝函式。因此,RegularExpressions 和 RegularExpressionsCore 單元都會使用 PCRE 正規表示式風格。

Delphi 的 RegularExpressions 單元

RegularExpressions 單元將 TRegEx、TMatch、TMatchCollection、TGroup 和 TGroupCollection 定義為記錄,而不是類別。這表示您不需要呼叫 Create 和 Free 來配置和釋放記憶體。

TRegEx 有個 Create 建構函數,如果你想要使用同一個正規表示式多次,你可以呼叫它。這樣 TRegEx 就不會編譯同一個正規表示式兩次。如果你呼叫建構函數,你就可以呼叫任何不以正規表示式為參數的非靜態方法。如果你沒有呼叫建構函數,你只能呼叫以正規表示式為參數的靜態(類別)方法。所有 TRegEx 方法都有靜態和非靜態的重載。你使用哪一個只取決於你是否想要使用同一個正規表示式呼叫 TRegEx 多次。

IsMatch 方法會接受一個字串並回傳 True 或 False,表示正規表示式是否符合(字串的一部分)。

Match 方法會接受一個字串並回傳一個包含第一個符合項目的詳細資料的 TMatch 記錄。如果符合失敗,它會回傳一個 TMatch 記錄,其中 Success 屬性設為 nil。 Match() 的非靜態重載會接受一個選用的起始位置和一個選用的長度參數,你可以使用它來只搜尋輸入字串的一部分。

Matches 方法會接受一個字串並回傳一個 TMatchCollection 記錄。這個記錄的預設 Item[] 屬性會包含一個 TMatch,表示正規表示式在字串中找到的每個符合項目。如果沒有任何符合項目,回傳的 TMatchCollection 記錄的 Count 屬性會是零。

使用 Replace 方法來搜尋並取代字串中的所有符合項目。你可以傳遞一個 TMatchEvaluator,它只是一個方法,它會接受一個稱為 Match 的 TMatch 類型的參數並回傳一個字串。你的方法回傳的字串會用作一個文字替換字串。如果你想要在使用 TMatchEvaluator 重載時替換字串中的反向參照,請在回傳字串之前呼叫提供的 Match 參數上的 Result 方法。

使用 Split 方法來沿著其 regex 比對分割字串。結果以動態字串陣列傳回。與 .NET 中相同,在正規表示式中由 擷取群組 比對的文字也包含在傳回的陣列中。如果您不喜歡這樣,請從您的 regex 中移除所有 命名擷取群組,並傳遞 roExplicitCapture 選項來停用編號擷取群組。 Split() 的非靜態重載會採用一個選用的 Count 參數,以指出傳回陣列可能擁有的最大元素數。換句話說,字串最多會分割 Count-1 次。擷取群組比對不包含在計算中。因此,如果您的 regex 具有擷取群組,傳回的陣列可能會有超過 Count 個元素。如果您傳遞 Count,您可以傳遞第二個選用參數,以指出字串中開始分割的位置。傳回陣列的第一個元素會傳回未分割的字串起始位置之前的部分。

TMatch 記錄提供幾個有關比對的詳細資訊的屬性。 Success 指出是否找到比對。如果這是 False,所有其他屬性和方法都是無效的。 Value 傳回比對的字串。 Index 和 Length 指出輸入字串中的位置和比對的長度。 Groups 傳回一個 TGroupCollection 記錄,它在預設的 Item[] 屬性中為每個擷取群組儲存一個 TGroup 記錄。您可以使用數字索引來 Item[] 編號擷取群組,並使用字串索引來 命名擷取群組。

TMatch 也提供兩個方法。 NextMatch 傳回這個之後的正規表示式的下一個比對。如果您的 TMatch 是 TMatchCollection 的一部分,您不應該使用 NextMatch 來取得下一個比對,而是應該使用 TMatchCollection.Item[],以避免重複搜尋。 TMatch.Result 採用一個參數,其中包含替換文字作為字串。它傳回如果您使用這個替換文字與 TRegEx.Replace,這個比對將會被替換的字串。

TGroup 記錄具有 Success、Value、Index 和 Length 屬性,它們的工作方式就像 TMatch 的那些屬性。

在 Delphi XE5 及之前版本中,TRegEx 始終會略過長度為零的比對。這已在 Delphi XE6 中修正。您可以透過修改 RegularExpressionsCore.pas 來移除 TPerlRegEx.Create 中的 State := [preNotEmpty] 行,在 XE5 及之前版本中進行相同的修正。這個變更也會影響直接使用 TPerlRegEx 而未設定 State 屬性的程式碼。

較舊版本的 Delphi 的正規表示式類別

TPerlRegEx 在 Embarcadero 授權一份副本以包含在 Delphi XE 中之前就已經可以使用了。根據您的需求,您可以下載兩個版本之一,以搭配 Delphi 2010 及更早版本使用。

  • 下載最新的基於類別的 TPerlRegEx
  • 下載較舊的基於元件的 TPerlRegEx

TPerlRegEx 的最新版本與 Delphi XE 中的 RegularExpressionsCore 單元完全相容。對於以 Delphi 2010 或更早版本編寫的新程式碼,強烈建議使用 TPerlRegEx 的最新版本。如果您稍後將程式碼移轉到 Delphi XE,您所要做的就是將 PerlRegEx 取代為單元的 uses 子句中的 RegularExrpessionsCore。

較舊版本的 TPerlRegEx 是非視覺元件。這表示您可以將 TPerlRegEx 放到元件面板上,並將它拖放到表單上。最初的 TPerlRegEx 是在 Borland 的目標是讓元件面板上的所有東西都有元件時開發的。

如果您想要從較舊版本的 TPerlRegEx 移轉到最新的 TPerlRegEx,請從移除您可能已放置在表單或資料模組上的任何 TPerlRegEx 元件開始,並在執行階段實例化物件。在執行階段實例化時,您不再需要將擁有者元件傳遞給 Create() 建構函式。只要移除參數即可。

最初的 TPerlRegEx 中的一些屬性和方法名稱有點難以處理。這些名稱已在最新的 TPerlRegEx 中重新命名。基本上,在所有識別碼中,SubExpression 已被 Group 取代,而 MatchedExpression 已被 Matched 取代。以下是已變更識別碼的完整清單

舊識別碼新識別碼
StoreSubExpressionStoreGroups
NamedSubExpressionNamedGroup
MatchedExpressionMatchedText
MatchedExpressionLengthMatchedLength
MatchedExpressionOffsetMatchedOffset
SubExpressionCountGroupCount
SubExpressionsGroups
SubExpressionLengthsGroupLengths
SubExpressionOffsetsGroupOffsets

UTF-8 與 UTF-16

您需要留意的其中一件事是,您可以在這裡下載的 TPerlRegEx 版本,以及包含在 Delphi XE、XE2 和 XE3 中的版本,都使用 UTF8String 屬性,而所有 Offset 和 Length 屬性都是這些 UTF-8 字串的索引。這是因為當時 PCRE 只支援 UTF-8,而使用 UTF8String 可以避免重複轉換。如果效能至關重要,您應該在這些 Delphi 版本中使用 TPerlRegEx,而不是 TRegEx。如果您的資料已經是 UTF-8,您可以將 UTF-8 直接傳遞給 TPerlRegEx。如果您的資料使用其他編碼,您可以控制轉換為 UTF-8 的時間,以避免重複轉換相同的資料。

在 Delphi XE4 和 XE5 中,TPerlRegEx 具有 UnicodeString(UTF-16)屬性,但仍然傳回 UTF-8 的偏移量和長度。在 Delphi XE6 中,Offset 和 Length 屬性已變更為 UTF-16 的偏移量和長度。這表示如果您的字串包含非 ASCII 字元,使用 Offset 和 Length 屬性的 XE3 或 XE6 相容程式碼將無法與 XE4 和 XE5 相容。從 Delphi XE4 到 Delphi 10 到 10.2,持續使用 UTF-8 版本的 PCRE,即使 PCRE 已經具有原生 UTF-16 支援。這與使用 UnicodeString 相結合,表示在 UTF-16 和 UTF-8 之間進行持續轉換,這可能會大幅降低正規表示式的效能,特別是對於長的主旨字串。

Delphi 10.3 和更新版本在 Windows 平台上使用 UTF-16 版本的 PCRE。TRegEx 和 TPerlRegEx 現在對所有內容都使用 UnicodeString,而不會轉換為 UTF-8。從 Delphi XE4 或更新版本升級到 10.3 或更新版本,絕對會改善任何使用 TRegEx 或 TPerlRegEx 的程式碼效能。從 Delphi XE3 或更早版本升級會改善效能,除非您對所有內容都使用 UTF-8。

與 Delphi Prism 搭配使用 System.Text.RegularExpressions

Delphi Prism 是 Embarcadero 的 Delphi 語言變體,專門開發為針對 .NET 架構。Delphi Prism 存在於 Visual Studio IDE 中。它完全基於 .NET 架構。在 Delphi Prism 中,您可以簡單地將 System.Text.RegularExpressions 命名空間新增到單元的 uses 子句。然後,您可以存取 .NET 正規表示式類別,例如 Regex、Match 和 Group。您可以使用 Delphi Prism,就像 C# 和 VB 開發人員可以使用它們一樣。

與 Delphi for .NET 搭配使用 System.Text.RegularExpressions

Delphi 8、2005、2006 和 2007 包含一個 Delphi for .NET 編譯器,用於開發 WinForms 和 VCL.NET 應用程式。儘管 Delphi for .NET 只支援 .NET 1.1 或 2.0(視您的 Delphi 版本而定),但您仍然可以使用 .NET 的完整正規表示式支援。您只需要將 System.Text.RegularExpressions 命名空間新增到單元的 uses 子句,就能夠存取所有 .NET 正規表示式類別。

Delphi Regular Expressions Classes
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » Delphi Regular Expressions Classes

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Delphi Regular Expressions Classes

Delphi XE is the first release of Delphi that has built-in support for regular expressions. In most cases you’ll use the RegularExpressions unit. This unit defines a set of records that mimic the regular expression classes in the .NET framework. Just like in .NET, they allow you to use a regular expression in just one line of code without explicit memory management.

Internally the RegularExpressions unit uses the RegularExpressionsCore unit which defines the TPerlRegEx class. TPerlRegEx is a wrapper around the open source PCRE library developed by the author of this website. Thus both the RegularExpressions and RegularExpressionsCore units use the PCRE regex flavor.

Delphi’s RegularExpressions unit

The RegularExpressions unit defines TRegEx, TMatch, TMatchCollection, TGroup, and TGroupCollection as records rather than as classes. That means you don’t need to call Create and Free to allocate and deallocate memory.

TRegEx does have a Create constructor that you can call if you want to use the same regular expression more than once. That way TRegEx doesn’t compile the same regex twice. If you call the constructor, you can then call any of the non-static methods that do not take the regular expression as a parameter. If you don’t call the constructor, you can only call the static (class) methods that take the regular expression as a parameter. All TRegEx methods have static and non-static overloads. Which ones you use solely depends on whether you want to make more than one call to TRegEx using the same regular expression.

The IsMatch method takes a string and returns True or False indicating whether the regular expression matches (part of) the string.

The Match method takes a string and returns a TMatch record with the details of the first match. If the match fails, it returns a TMatch record with the Success property set to nil. The non-static overload of Match() takes an optional starting position and an optional length parameter that you can use to search through only part of the input string.

The Matches method takes a string and returns a TMatchCollection record. The default Item[] property of this record holds a TMatch for each match the regular expression found in the string. If there are no matches, the Count property of the returned TMatchCollection record is zero.

Use the Replace method to search-and-replace all matches in a string. You can pass a TMatchEvaluator which is nothing more than a method that takes one parameter called Match of type TMatch and returns a string. The string returned by your method is used as a literal replacement string. If you want backreferences in your string to be replaced when using the TMatchEvaluator overload, call the Result method on the provided Match parameter before returning the string.

Use the Split method to split a string along its regex matches. The result is returned as a dynamic array of strings. As in .NET, text matched by capturing groups in the regular expression are also included in the returned array. If you don’t like this, remove all named capturing groups from your regex and pass the roExplicitCapture option to disable numbered capturing groups. The non-static overload of Split() takes an optional Count parameter to indicate the maximum number of elements that the returned array may have. In other words, the string is split at most Count-1 times. Capturing group matches are not included in the count. So if your regex has capturing groups, the returned array may have more than Count elements. If you pass Count, you can pass a second optional parameter to indicate the position in the string at which to start splitting. The part of the string before the starting position is returned unsplit in the first element of the returned array.

The TMatch record provides several properties with details about the match. Success indicates if a match was found. If this is False, all other properties and methods are invalid. Value returns the matched string. Index and Length indicate the position in the input string and the length of the match. Groups returns a TGroupCollection record that stores a TGroup record in its default Item[] property for each capturing group. You can use a numeric index to Item[] for numbered capturing groups, and a string index for named capturing groups.

TMatch also provides two methods. NextMatch returns the next match of the regular expression after this one. If your TMatch is part of a TMatchCollection you should not use NextMatch to get the next match but use TMatchCollection.Item[] instead, in order to avoid repeating the search. TMatch.Result takes one parameter with the replacement text as a string. It returns the string that this match would have been replaced with if you had used this replacement text with TRegEx.Replace.

The TGroup record has Success, Value, Index and Length properties that work just like those of the TMatch.

In Delphi XE5 and prior TRegEx always skips zero-length matches. This was fixed in Delphi XE6. You can make the same fix in XE5 and prior by modifying RegularExpressionsCore.pas to remove the line State := [preNotEmpty] from TPerlRegEx.Create. This change will also affect code that uses TPerlRegEx directly without setting the State property.

Regular Expressions Classes for Older Versions of Delphi

TPerlRegEx has been available long before Embarcadero licensed a copy for inclusion with Delphi XE. Depending on your needs, you can download one of two versions for use with Delphi 2010 and earlier.

  • Download the latest class-based TPerlRegEx
  • Download the older component-based TPerlRegEx

The latest release of TPerlRegEx is fully compatible with the RegularExpressionsCore unit in Delphi XE. For new code written in Delphi 2010 or earlier, using the latest release of TPerlRegEx is strongly recommended. If you later migrate your code to Delphi XE, all you have to do is replace PerlRegEx with RegularExrpessionsCore in the uses clause of your units.

The older versions of TPerlRegEx are non-visual components. This means you can put TPerlRegEx on the component palette and drop it on a form. The original TPerlRegEx was developed when Borland’s goal was to have a component for everything on the component palette.

If you want to migrate from an older version of TPerlRegEx to the latest TPerlRegEx, start with removing any TPerlRegEx components you may have placed on forms or data modules and instantiate the objects at runtime instead. When instantiating at runtime, you no longer need to pass an owner component to the Create() constructor. Simply remove the parameter.

Some of the property and method names in the original TPerlRegEx were a bit unwieldy. These have been renamed in the latest TPerlRegEx. Essentially, in all identifiers SubExpression was replaced with Group and MatchedExpression was replaced with Matched. Here is a complete list of the changed identifiers:

Old IdentifierNew Identifier
StoreSubExpressionStoreGroups
NamedSubExpressionNamedGroup
MatchedExpressionMatchedText
MatchedExpressionLengthMatchedLength
MatchedExpressionOffsetMatchedOffset
SubExpressionCountGroupCount
SubExpressionsGroups
SubExpressionLengthsGroupLengths
SubExpressionOffsetsGroupOffsets

UTF-8 Versus UTF-16

One thing you need to watch out for is that the TPerlRegEx versions you can download here as well as those included with Delphi XE, XE2, and XE3 use UTF8String properties and all the Offset and Length properties are indexes to those UTF-8 strings. This is because at that time PCRE only supported UTF-8 and using UTF8String avoids repeated conversions. If performance is critical, you should use TPerlRegEx instead of TRegEx with these versions of Delphi. If your data is already UTF-8, you can pass the UTF-8 directly to TPerlRegEx. If your data uses another encoding, you can control when the conversion to UTF-8 happens to avoid repeated conversions of the same data.

In Delphi XE4 and XE5 TPerlRegEx has UnicodeString (UTF-16) properties but still returns UTF-8 offsets and lengths. In Delphi XE6 the Offset and Length properties were changed to UTF-16 offsets and lengths. This means that code that works with XE3 or XE6 that uses the Offset and Length properties will not work with XE4 and XE5 if your strings contain non-ASCII characters. Delphi XE4 through and Delphi 10 through 10.2 continued to use the UTF-8 version of PCRE even though PCRE already had native UTF-16 support. This combined with the use of UnicodeString means constant conversions between UTF-16 and UTF-8 which can significantly degrade regex performance, particularly with long subject strings.

Delphi 10.3 and later use the UTF-16 version of PCRE on the Windows platform. TRegEx and TPerlRegEx now use UnicodeString for everything, without any conversion to UTF-8. Upgrading from Delphi XE4 or later to 10.3 or later will definitely improve the performance of any code that uses TRegEx or TPerlRegEx. Upgrading from Delphi XE3 or prior will improve performance unless you were doing everything with UTF-8.

Use System.Text.RegularExpressions with Delphi Prism

Delphi Prism was Embarcadero’s variant of the Delphi language specifically developed to target the .NET framework. Delphi Prism lived inside the Visual Studio IDE. It was based entirely on the .NET framework. In Delphi Prism you could simply add the System.Text.RegularExpressions namespace to the uses clause of your units. Then you could access the .NET regex classes such as Regex, Match, and Group. You could them with Delphi Prism just as they can be used by C# and VB developers.

Use System.Text.RegularExpressions with Delphi for .NET

Delphi 8, 2005, 2006, and 2007 included a Delphi for .NET compiler for developing WinForms and VCL.NET applications. Though Delphi for .NET only supported .NET 1.1 or 2.0, depending on your Delphi version, you could still use .NET’s full regular expression support. You only needed to add the System.Text.RegularExpressions namespace to the uses clause of your units to be able to access all the .NET regex classes.

©2015-2025 艾丽卡 support@alaica.com