如何在 Xojo (REALbasic) 中使用正则表达式如何在 Xojo (REALbasic) 中使用正则表达式如何在 Xojo (REALbasic) 中使用正则表达式如何在 Xojo (REALbasic) 中使用正则表达式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

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

Regex 工具
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
更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序与语言
正则表达式范例
正则表达式参考
替换字符串参考

如何在 Xojo (REALbasic) 中使用正则表达式

Xojo,以前称为 REALbasic,包含内置的 RegEx 类别。在内部,这个类别是基于开源的 PCRE 函数库。对你这个 Xojo 开发人员来说,这表示 RegEx 类别会提供给你丰富的 Perl 兼容正则表达式。本网站上的 正则表达式教程 没有明确提到 Xojo。教学中所述关于 PCRE 的 regex 风格,也适用于 Xojo。唯一的例外是 不区分大小写 和「多行」比对模式。在 PCRE 中,它们缺省为关闭,而在 Xojo 中,它们缺省为打开。

Xojo 使用 PCRE 的 UTF-8 版本。这表示,如果您要处理从文件或网络截取的非 ASCII 数据,您需要使用 Xojo 的 TextConverter 类别将字符串转换成 UTF-8,然后再传递给 RegEx 对象。您也需要使用 TextConverter 将 RegEx 类别传回的字符串从 UTF-8 转换回您的应用程序使用的编码。

RegEx 类别

若要使用正则表达式,您需要创建 RegEx 类别的新运行个体。将您的正则表达式指定给 SearchPattern 属性。您可以在 Options 属性中设置各种选项,这是 RegExOptions 类别的运行个体。

若要检查正则表达式是否符合特定字符串,请调用 RegEx 对象的 Search 方法,并将主旨字符串传递为参数。如果找到符合项,此方法会传回 RegExMatch 类别的运行个体,如果找不到符合项,则传回 Nil。若要在同一个主旨字符串中找到第二个符合项,请再次调用 Search 方法,而不要传递任何参数。不要再次传递主旨字符串,因为这样会从字符串的开头重新开始搜索。持续调用 Search,而不要传递任何参数,直到它传回 Nil,以反复运算字符串中所有正则表达式符合项。

RegExMatch 类别

当 Regex.Search 方法找到一个符合项时,它会将符合项的详细数据保存在 RegExMatch 对象中。此对象有三个属性。SubExpressionCount 属性会传回正则表达式中 捕获组 的数量加上一。例如,它会传回正则表达式 (1)(2) 的 3。SubExpressionString 属性会传回由正则表达式或捕获组符合的子字符串。SubExpressionString(0) 会传回整个正则表达式符合项,而 SubExpressionString(1) 到 SubExpressionString(SubExpressionCount-1) 会传回捕获组的符合项。SubExpressionStartB 会传回整个正则表达式或其中一个捕获组符合项开始的字节偏移量,具体取决于您传递给属性的数字索引作为参数。

RegExOptions 类别

RegExOptions 类别有九个属性,可为您的正则表达式设置各种选项。

  • 将 CaseSensitive (缺省为 False) 设置为 True,以将大写和小写字母视为不同的字符。此选项是其他编程语言中「大小写不敏感模式」或 /i 的相反。
  • 将 DotMatchAll (缺省为 False) 设置为 True,以让 点 符合所有字符,包括换行字符。此选项等同于其他编程语言中的「单行模式」或 /s。
  • 如果您希望量词是惰性的,请将 Greedy (缺省为 True) 设置为 False,实际上使 .* 与 .*? 相同。我强烈建议不要将 Greedy 设置为 False。相反,请改用 .*? 语法。这样,当有人只查看正则表达式时,他们会清楚地看到您何时使用贪婪量词,以及何时使用惰性量词。
  • LineEndType 选项是唯一一个采用整数而非布尔值的选项。此选项会影响 插入符号和美元 视为「行尾」字符的字符。默认值为 0,它接受 \r 和 \n 作为行尾字符。将其设置为 1 以自动侦测主机平台,并在您的应用程序运行于 Windows 和 Linux 时使用 \n,在 Mac 上运行时使用 \r。将其设置为 2 表示 Mac (\r)、3 表示 Windows (\n) 和 4 表示 UNIX (\n)。我建议您将此选项保留为零,这最有可能提供您预期的结果。此选项实际上是 Xojo 中对 PCRE 函数库的修改。PCRE 仅支持选项 4,这常常让 Windows 开发人员感到困惑,因为它会导致 test$ 与 test\r\n 不符合,因为 Windows 使用 \r\n 作为换行符号。
  • 如果您希望略过长度为零的符合项,请将 MatchEmpty (缺省为 True) 设置为 False。
  • 如果您希望 Regex.Replace 方法在主旨字符串中搜索并取代所有正则表达式比对,而非仅取代第一个比对,请将 ReplaceAllMatches 设为 True(缺省为 False)。
  • 如果您不希望将字符串开头视为行开头,请将 StringBeginIsLineBegin(缺省为 True)设为 False。如果您正在处理一大块数据作为数个独立字符串,而只有第一个字符串应视为开始(概念上的)整体字符串,这会很有用。
  • 类似地,如果您传递至 Search 方法的字符串并非您正在处理的整体数据块的真正结尾,请将 StringEndIsLineEnd(缺省为 True)设为 False。
  • 将 TreatTargetAsOneLine(缺省为 False)设为 True,可让插入符号和美元符号仅在字符串的开头和结尾比对。缺省情况下,它们也会在嵌入式换行符号之后和之前比对。此选项是其他编程语言中「多行模式」或 /m 的反向。

Xojo RegEx 原代码范例

'Prepare a regular expression object
Dim myRegEx As RegEx
Dim myMatch As RegExMatch
myRegEx = New RegEx
myRegEx.Options.TreatTargetAsOneLine = True
myRegEx.SearchPattern = "regex"
'Pop up all matches one by one
myMatch = myRegEx.Search(SubjectString)
While myMatch <> Nil
	MsgBox(myMatch.SubExpressionString(0))
	myMatch = myRegEx.Search()
Wend

搜索和取代

除了在字符串中寻找正则表达式比对之外,您还可以将比对取代为另一个字符串。为此,请设置 RegEx 对象的 ReplacementPattern 属性,然后调用 Replace 方法。将来源字符串作为参数传递至 Replace 方法。此方法会传回已套用取代的字符串副本。RegEx.Options.ReplaceAllMatches 属性会判断是否仅取代第一个正则表达式比对,或是否取代所有正则表达式比对。

在 ReplacementPattern 字符串中,您可以使用 $&、$0 或 \0 将整个正则表达式比对插入取代中。对于第一个捕获组的比对,请使用 $1 或 \1;对于第二个比对,请使用 $2 或 \2,以此类推。

如果您希望更进一步控制取代方式,您可以像上述代码片段中一样,反复运行正则表达式比对,并针对每个比对调用 RegExMatch.Replace 方法。此方法有点名不符实,因为它实际上并未取代任何内容。相反地,它会传回 RegEx.ReplacementPattern 字符串,其中所有对比对和捕获组的参照都已取代。您可以使用这些结果自行进行取代。如果您希望收集每个正则表达式比对的捕获组组合,此方法也很有用。

如何在 Xojo (REALbasic) 中使用正則表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » 如何在 Xojo (REALbasic) 中使用正則表示式

Regex 工具
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
本網站的更多資訊
簡介
正則表示式快速入門
正則表示式教學
替換字串教學
應用程式與語言
正則表示式範例
正則表示式參考
替換字串參考

如何在 Xojo (REALbasic) 中使用正則表示式

Xojo,以前稱為 REALbasic,包含內建的 RegEx 類別。在內部,這個類別是基於開源的 PCRE 函式庫。對你這個 Xojo 開發人員來說,這表示 RegEx 類別會提供給你豐富的 Perl 相容正則表示式。本網站上的 正則表示式教學 沒有明確提到 Xojo。教學中所述關於 PCRE 的 regex 風格,也適用於 Xojo。唯一的例外是 不區分大小寫 和「多行」比對模式。在 PCRE 中,它們預設為關閉,而在 Xojo 中,它們預設為開啟。

Xojo 使用 PCRE 的 UTF-8 版本。這表示,如果您要處理從檔案或網路擷取的非 ASCII 資料,您需要使用 Xojo 的 TextConverter 類別將字串轉換成 UTF-8,然後再傳遞給 RegEx 物件。您也需要使用 TextConverter 將 RegEx 類別傳回的字串從 UTF-8 轉換回您的應用程式使用的編碼。

RegEx 類別

若要使用正規表示式,您需要建立 RegEx 類別的新執行個體。將您的正規表示式指定給 SearchPattern 屬性。您可以在 Options 屬性中設定各種選項,這是 RegExOptions 類別的執行個體。

若要檢查正規表示式是否符合特定字串,請呼叫 RegEx 物件的 Search 方法,並將主旨字串傳遞為參數。如果找到符合項,此方法會傳回 RegExMatch 類別的執行個體,如果找不到符合項,則傳回 Nil。若要在同一個主旨字串中找到第二個符合項,請再次呼叫 Search 方法,而不要傳遞任何參數。不要再次傳遞主旨字串,因為這樣會從字串的開頭重新開始搜尋。持續呼叫 Search,而不要傳遞任何參數,直到它傳回 Nil,以反覆運算字串中所有正規表示式符合項。

RegExMatch 類別

當 Regex.Search 方法找到一個符合項時,它會將符合項的詳細資料儲存在 RegExMatch 物件中。此物件有三個屬性。SubExpressionCount 屬性會傳回正規表示式中 擷取群組 的數量加上一。例如,它會傳回正規表示式 (1)(2) 的 3。SubExpressionString 屬性會傳回由正規表示式或擷取群組符合的子字串。SubExpressionString(0) 會傳回整個正規表示式符合項,而 SubExpressionString(1) 到 SubExpressionString(SubExpressionCount-1) 會傳回擷取群組的符合項。SubExpressionStartB 會傳回整個正規表示式或其中一個擷取群組符合項開始的位元組偏移量,具體取決於您傳遞給屬性的數字索引作為參數。

RegExOptions 類別

RegExOptions 類別有九個屬性,可為您的正規表示式設定各種選項。

  • 將 CaseSensitive (預設為 False) 設定為 True,以將大寫和小寫字母視為不同的字元。此選項是其他程式語言中「大小寫不敏感模式」或 /i 的相反。
  • 將 DotMatchAll (預設為 False) 設定為 True,以讓 點 符合所有字元,包括換行字元。此選項等同於其他程式語言中的「單行模式」或 /s。
  • 如果您希望量詞是惰性的,請將 Greedy (預設為 True) 設定為 False,實際上使 .* 與 .*? 相同。我強烈建議不要將 Greedy 設定為 False。相反,請改用 .*? 語法。這樣,當有人只查看正規表示式時,他們會清楚地看到您何時使用貪婪量詞,以及何時使用惰性量詞。
  • LineEndType 選項是唯一一個採用整數而非布林值的選項。此選項會影響 插入符號和美元 視為「行尾」字元的字元。預設值為 0,它接受 \r 和 \n 作為行尾字元。將其設定為 1 以自動偵測主機平台,並在您的應用程式執行於 Windows 和 Linux 時使用 \n,在 Mac 上執行時使用 \r。將其設定為 2 表示 Mac (\r)、3 表示 Windows (\n) 和 4 表示 UNIX (\n)。我建議您將此選項保留為零,這最有可能提供您預期的結果。此選項實際上是 Xojo 中對 PCRE 函式庫的修改。PCRE 僅支援選項 4,這常常讓 Windows 開發人員感到困惑,因為它會導致 test$ 與 test\r\n 不符合,因為 Windows 使用 \r\n 作為換行符號。
  • 如果您希望略過長度為零的符合項,請將 MatchEmpty (預設為 True) 設定為 False。
  • 如果您希望 Regex.Replace 方法在主旨字串中搜尋並取代所有正規表示式比對,而非僅取代第一個比對,請將 ReplaceAllMatches 設為 True(預設為 False)。
  • 如果您不希望將字串開頭視為行開頭,請將 StringBeginIsLineBegin(預設為 True)設為 False。如果您正在處理一大塊資料作為數個獨立字串,而只有第一個字串應視為開始(概念上的)整體字串,這會很有用。
  • 類似地,如果您傳遞至 Search 方法的字串並非您正在處理的整體資料塊的真正結尾,請將 StringEndIsLineEnd(預設為 True)設為 False。
  • 將 TreatTargetAsOneLine(預設為 False)設為 True,可讓插入符號和美元符號僅在字串的開頭和結尾比對。預設情況下,它們也會在嵌入式換行符號之後和之前比對。此選項是其他程式語言中「多行模式」或 /m 的反向。

Xojo RegEx 原始碼範例

'Prepare a regular expression object
Dim myRegEx As RegEx
Dim myMatch As RegExMatch
myRegEx = New RegEx
myRegEx.Options.TreatTargetAsOneLine = True
myRegEx.SearchPattern = "regex"
'Pop up all matches one by one
myMatch = myRegEx.Search(SubjectString)
While myMatch <> Nil
	MsgBox(myMatch.SubExpressionString(0))
	myMatch = myRegEx.Search()
Wend

搜尋和取代

除了在字串中尋找正規表示式比對之外,您還可以將比對取代為另一個字串。為此,請設定 RegEx 物件的 ReplacementPattern 屬性,然後呼叫 Replace 方法。將來源字串作為參數傳遞至 Replace 方法。此方法會傳回已套用取代的字串副本。RegEx.Options.ReplaceAllMatches 屬性會判斷是否僅取代第一個正規表示式比對,或是否取代所有正規表示式比對。

在 ReplacementPattern 字串中,您可以使用 $&、$0 或 \0 將整個正規表示式比對插入取代中。對於第一個擷取群組的比對,請使用 $1 或 \1;對於第二個比對,請使用 $2 或 \2,以此類推。

如果您希望更進一步控制取代方式,您可以像上述程式碼片段中一樣,反覆執行正規表示式比對,並針對每個比對呼叫 RegExMatch.Replace 方法。此方法有點名不符實,因為它實際上並未取代任何內容。相反地,它會傳回 RegEx.ReplacementPattern 字串,其中所有對比對和擷取群組的參照都已取代。您可以使用這些結果自行進行取代。如果您希望收集每個正規表示式比對的擷取群組組合,此方法也很有用。

How to Use Regular Expressions in Xojo (REALbasic)
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » How to Use Regular Expressions in Xojo (REALbasic)

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

How to Use Regular Expressions in Xojo (REALbasic)

Xojo, formerly known as REALbasic, includes a built-in RegEx class. Internally, this class is based on the open source PCRE library. What this means to you as a Xojo developer is that the RegEx class provides you with a rich flavor of Perl-compatible regular expressions. The regular expressions tutorial on this website does not explicitly mention Xojo. Everything said in the tutorial about PCRE’s regex flavor also applies to Xojo. The only exception are the case insensitive and “multi-line” matching modes. In PCRE, they’re off by default, while in Xojo they’re on by default.

Xojo uses the UTF-8 version of PCRE. This means that if you want to process non-ASCII data that you’ve retrieved from a file or the network, you’ll need to use Xojo’s TextConverter class to convert your strings into UTF-8 before passing them to the RegEx object. You’ll also need to use the TextConverter to convert the strings returned by the RegEx class from UTF-8 back into the encoding your application is working with.

The RegEx Class

To use a regular expression, you need to create a new instance of the RegEx class. Assign your regular expression to the SearchPattern property. You can set various options in the Options property, which is an instance of the RegExOptions class.

To check if a regular expression matches a particular string, call the Search method of the RegEx object, and pass the subject string as a parameter. This method returns an instance of the RegExMatch class if a match is found, or Nil if no match is found. To find the second match in the same subject string, call the Search method again, without any parameters. Do not pass the subject string again, since doing so restarts the search from the beginning of the string. Keep calling Search without any parameters until it returns Nil to iterate over all regular expression matches in the string.

The RegExMatch Class

When the Regex.Search method finds a match, it stores the match’s details in a RegExMatch object. This object has three properties. The SubExpressionCount property returns the number of capturing groups in the regular expression plus one. E.g. it returns 3 for the regex (1)(2). The SubExpressionString property returns the substring matched by the regular expression or a capturing group. SubExpressionString(0) returns the whole regex match, while SubExpressionString(1) through SubExpressionString(SubExpressionCount-1) return the matches of the capturing group. SubExpressionStartB returns the byte offset of the start of the match of the whole regex or one of the capturing groups depending on the numeric index you pass as a parameter to the property.

The RegExOptions Class

The RegExOptions class has nine properties to set various options for your regular expression.

  • Set CaseSensitive (False by default) to True to treat uppercase and lowercase letters as different characters. This option is the inverse of “case insensitive mode” or /i in other programming languages.
  • Set DotMatchAll (False by default) to True to make the dot match all characters, including line break characters. This option is the equivalent of “single line mode” or /s in other programming languages.
  • Set Greedy (True by default) to False if you want quantifiers to be lazy, effectively making .* the same as .*?. I strongly recommend against setting Greedy to False. Simply use the .*? syntax instead. This way, somebody reading your source code will clearly see when you’re using greedy quantifiers and when you’re using lazy quantifiers when they look only at the regular expression.
  • The LineEndType option is the only one that takes an Integer instead of a Boolean. This option affect which character the caret and dollar treat as the “end of line” character. The default is 0, which accepts both \r and \n as end-of-line characters. Set it to 1 to use auto-detect the host platform, and use \n when your application runs on Windows and Linux, and \r when it runs on a Mac. Set it to 2 for Mac (\r), 3 for Windows (\n) and 4 for UNIX (\n). I recommend you leave this option as zero, which is most likely to give you the results you intended. This option is actually a modification to the PCRE library made in Xojo. PCRE supports only option 4, which often confuses Windows developers since it causes test$ to fail against test\r\n as Windows uses \r\n for line breaks.
  • Set MatchEmpty (True by default) to False if you want to skip zero-length matches.
  • Set ReplaceAllMatches (False by default) to True if you want the Regex.Replace method to search-and-replace all regex matches in the subject string rather than just the first one.
  • Set StringBeginIsLineBegin (True by default) to False if you don’t want the start of the string to be considered the start of the line. This can be useful if you’re processing a large chunk of data as several separate strings, where only the first string should be considered as starting the (conceptual) overall string.
  • Similarly, set StringEndIsLineEnd (True by default) to False if the string you’re passing to the Search method isn’t really the end of the whole chunk of data you’re processing.
  • Set TreatTargetAsOneLine (False by default) to make the caret and dollar match at the start and the end of the string only. By default, they will also match after and before embedded line breaks. This option is the inverse of the “multi-line mode” or /m in other programming languages.

Xojo RegEx Source Code Example

'Prepare a regular expression object
Dim myRegEx As RegEx
Dim myMatch As RegExMatch
myRegEx = New RegEx
myRegEx.Options.TreatTargetAsOneLine = True
myRegEx.SearchPattern = "regex"
'Pop up all matches one by one
myMatch = myRegEx.Search(SubjectString)
While myMatch <> Nil
	MsgBox(myMatch.SubExpressionString(0))
	myMatch = myRegEx.Search()
Wend

Searching and Replacing

In addition to finding regex matches in a string, you can replace the matches with another string. To do so, set the ReplacementPattern property of your RegEx object, and then call the Replace method. Pass the source string as a parameter to the Replace method. The method will return a copy of the string with the replacement(s) applied. The RegEx.Options.ReplaceAllMatches property determines if only the first regex match or if all regex matches will be replaced.

In the ReplacementPattern string, you can use $&, $0 or \0 to insert the whole regular expression match into the replacement. Use $1 or \1 for the match of the first capturing group, $2 or \2 for the second, etc.

If you want more control over how the replacements are made, you can iterate over the regex matches like in the code snippet above, and call the RegExMatch.Replace method for each match. This method is a bit of a misnomer, since it doesn’t actually replace anything. Rather, it returns the RegEx.ReplacementPattern string with all references to the match and capturing groups substituted. You can use this results to make the replacements on your own. This method is also useful if you want to collect a combination of capturing groups for each regex match.

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