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

加载更多搜索结果...

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

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

如何在 Visual Basic 中使用正则表达式

与可以访问 .NET 优异的 正则表达式支持 的 Visual Basic.NET 不同,老旧的 Visual Basic 6 本身并未附带任何正则表达式支持。不过,VB6 确实让使用 ActiveX 和 COM 函数库提供的功能变得非常容易。

其中一个此类函数库是 Microsoft 的 VBScript 脚本函数库,从版本 5.5 开始具备良好的正则表达式功能。它实作 JavaScript 中使用的正则表达式风格。此函数库是 Internet Explorer 5.5 和更新版本的组成部分。它在所有运行 Windows XP、Vista、7、8、8.1 或 10 的电脑上都可用,如果用户升级到 IE 5.5 或更新版本,则在先前版本的 Windows 上也可用。这包括用于连接到互联网的几乎所有 Windows 电脑。

Internet Explorer 5.5 在网络标准方面并未获得很高的分数。它的 JavaScript 正则表达式实作与实际标准之间存在不少差异。幸运的是,大多数都是不太可能影响您的特殊情况。IE 的现代版本在奇异模式中呈现网页时,仍会使用 IE 5.5 实作。在标准模式中,IE 的现代版本会非常严格地遵循 JavaScript 标准。即使已安装 IE 的现代版本,VBScript 正则表达式仍会使用 IE 5.5 实作。

要在 Visual Basic 应用程序中使用此函数库,请在 VB IDE 的功能表中选择 [项目|参照]。向下卷动清单,找到项目「Microsoft VBScript Regular Expressions 5.5」。它位于「Microsoft VBScript Regular Expressions 1.0」项目的正下方。请务必勾选 5.5 版本,而非 1.0 版本。1.0 版本仅提供向后兼容性。它的功能并不令人满意。

添加参照后,您可以查看函数库提供的类别和类别成员。在功能表中选择 [查看|对象浏览器]。在对象浏览器中,选择左上角下拉清单中的「VBScript_RegExp_55」函数库。如需详细说明,请参阅本网站上的 VBScript 正则表达式参考。在 教学 中针对 JavaScript 正则表达式风格所说的任何内容也适用于 VBScript 的风格。唯一的例外是 替换文本 中对 \xFF 和 \uFFFF 字符转义的支持。JavaScript 在字符串文本中支持这些字符,但 Visual Basic 不支持。

VB6 与 VBScript 之间唯一的差异在于,您需要使用 Dim 陈述式在创建对象之前声明对象。以下是完整的代码片段。这是 VBScript 页面 上的两个代码片段组合而成,并添加了三个 Dim 陈述式。

'Prepare a regular expression object
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  MsgBox(myMatch.Value)
Next
如何在 Visual Basic 中使用正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » 如何在 Visual Basic 中使用正規表示式

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
本網站的更多內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

如何在 Visual Basic 中使用正規表示式

與可以存取 .NET 優異的 正規表示式支援 的 Visual Basic.NET 不同,老舊的 Visual Basic 6 本身並未附帶任何正規表示式支援。不過,VB6 確實讓使用 ActiveX 和 COM 函式庫提供的功能變得非常容易。

其中一個此類函式庫是 Microsoft 的 VBScript 腳本函式庫,從版本 5.5 開始具備良好的正規表示式功能。它實作 JavaScript 中使用的正規表示式風格。此函式庫是 Internet Explorer 5.5 和更新版本的組成部分。它在所有執行 Windows XP、Vista、7、8、8.1 或 10 的電腦上都可用,如果使用者升級到 IE 5.5 或更新版本,則在先前版本的 Windows 上也可用。這包括用於連線到網際網路的幾乎所有 Windows 電腦。

Internet Explorer 5.5 在網路標準方面並未獲得很高的分數。它的 JavaScript 正規表示式實作與實際標準之間存在不少差異。幸運的是,大多數都是不太可能影響您的特殊情況。IE 的現代版本在奇異模式中呈現網頁時,仍會使用 IE 5.5 實作。在標準模式中,IE 的現代版本會非常嚴格地遵循 JavaScript 標準。即使已安裝 IE 的現代版本,VBScript 正規表示式仍會使用 IE 5.5 實作。

要在 Visual Basic 應用程式中使用此函式庫,請在 VB IDE 的功能表中選擇 [專案|參照]。向下捲動清單,找到項目「Microsoft VBScript Regular Expressions 5.5」。它位於「Microsoft VBScript Regular Expressions 1.0」項目的正下方。請務必勾選 5.5 版本,而非 1.0 版本。1.0 版本僅提供向後相容性。它的功能並不令人滿意。

新增參照後,您可以查看函式庫提供的類別和類別成員。在功能表中選擇 [檢視|物件瀏覽器]。在物件瀏覽器中,選擇左上角下拉清單中的「VBScript_RegExp_55」函式庫。如需詳細說明,請參閱本網站上的 VBScript 正規表示式參考。在 教學 中針對 JavaScript 正規表示式風格所說的任何內容也適用於 VBScript 的風格。唯一的例外是 替換文字 中對 \xFF 和 \uFFFF 字元跳脫的支援。JavaScript 在字串文字中支援這些字元,但 Visual Basic 不支援。

VB6 與 VBScript 之間唯一的差異在於,您需要使用 Dim 陳述式在建立物件之前宣告物件。以下是完整的程式碼片段。這是 VBScript 頁面 上的兩個程式碼片段組合而成,並新增了三個 Dim 陳述式。

'Prepare a regular expression object
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  MsgBox(myMatch.Value)
Next
How to Use Regular Expressions in Visual Basic
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » How to Use Regular Expressions in Visual Basic

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 Visual Basic

Unlike Visual Basic.NET, which has access to the excellent regular expression support of .NET, good old Visual Basic 6 does not ship with any regular expression support. However, VB6 does make it very easy to use functionality provided by ActiveX and COM libraries.

One such library is Microsoft’s VBScript scripting library, which has decent regular expression capabilities starting with version 5.5. It implements the regular expression flavor used in JavaScript. This library is part of Internet Explorer 5.5 and later. It is available on all computers running Windows XP, Vista, 7, 8, 8.1, or 10, and previous versions of Windows if the user upgraded to IE 5.5 or later. That includes almost every Windows PC that is used to connect to the Internet.

Internet Explorer 5.5 did not score very high on web standards. There are quite a few differences between its implementation of JavaScript regular expressions and the actual standard. Fortunately, most are corner cases that are not likely to affect you. Modern versions of IE still use the IE 5.5 implementation when rendering web pages in quirks mode. In standards mode, modern versions of IE follow the JavaScript standard very closely. VBScript regular expressions also still use the IE 5.5 implementation, even when a modern version of IE is installed.

To use this library in your Visual Basic application, select Project|References in the VB IDE’s menu. Scroll down the list to find the item “Microsoft VBScript Regular Expressions 5.5”. It’s immediately below the “Microsoft VBScript Regular Expressions 1.0” item. Make sure to tick the 5.5 version, not the 1.0 version. The 1.0 version is only provided for backward compatibility. Its capabilities are less than satisfactory.

After adding the reference, you can see which classes and class members the library provides. Select View|Object Browser in the menu. In the Object Browser, select the “VBScript_RegExp_55” library in the drop-down list in the upper left corner. For a detailed description, see the VBScript regular expression reference on this website. Anything said about JavaScript’s flavor of regular expressions in the tutorial also applies to VBScript’s flavor. The only exception is the character escape support for \xFF and \uFFFF in the replacement text. JavaScript supports these in string literals, but Visual Basic does not.

The only difference between VB6 and VBScript is that you’ll need to use a Dim statement to declare the objects prior to creating them. Here’s a complete code snippet. It’s the two code snippets on the VBScript page put together, with three Dim statements added.

'Prepare a regular expression object
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "regex"
Set myMatches = myRegExp.Execute(subjectString)
For Each myMatch in myMatches
  MsgBox(myMatch.Value)
Next
©2015-2025 艾丽卡 support@alaica.com