使用 Boost 的 C++ 正则表达式使用 Boost 的 C++ 正则表达式使用 Boost 的 C++ 正则表达式使用 Boost 的 C++ 正则表达式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

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

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

使用 Boost 的 C++ 正则表达式

Boost 是 C++ 的免费原代码函数库。在下载并解压缩后,您需要运行 bootstrap 批量档或脚本,然后运行 b2 --with-regex 来编译 Boost 的正则表达式函数库。接着将您解压缩 Boost 的文件夹添加至 C++ 编译器的包含路径中。将该文件夹的 stage\lib 子文件夹添加至链接器的函数库路径中。然后您可以在 C++ 代码中添加 #include <boost/regex.hpp> 来使用 Boost 正则表达式。

如果您使用 C++Builder,您应该从 Embarcadero 下载适用于您特定 C++Builder 版本的 Boost 函数库。您取得的 Boost 版本取决于您的 C++Builder 版本,以及您锁定的目标是 Win32 还是 Win64。XE3 到 XE8 中的 Win32 编译器,以及 C++Builder 10 Seattle 到 10.1 Berlin 中的传统 Win32 编译器都停留在 Boost 1.39。XE3 到 XE6 中的 Win64 编译器使用 Boost 1.50。XE7 到 10.1 Berlin 中的 Win64 编译器使用 Boost 1.55。C++Builder 10 及更新版本中的新 C++11 Win32 编译器使用与 Win64 编译器相同的 Boost 版本。

本网站涵盖 Boost 1.38、1.39 和 1.42 到最新的 1.73。Boost 1.40 引进许多新的正则表达式功能,这些功能取自 Perl 5.10。但它也引进了一些严重的错误,直到 Boost 1.42 才修正。因此我们完全忽略 Boost 1.40 和 1.41。我们仍涵盖 Boost 1.38 和 1.39(它们具有相同的正则表达式功能),因为传统的 Win32 C++Builder 编译器停留在这个版本。如果您使用其他编译器,您绝对应该使用 Boost 1.42 或更新版本,以避免现在已成为旧错误的问题。您最好使用 Boost 1.47 或更新版本,因为这个版本会变更涉及反向引用的特定行为,如果您稍后从 1.47 之前的版本升级到 1.47 之后的版本,可能会变更部分正则表达式的行为。

在实务上,您大多会使用 Boost 的 ECMAScript 语法。它是缺省语法,提供的功能远多于其他语法。每当本网站上的教学提到 Boost 但未提到任何语法时,写下的内容就适用于 ECMAScript 语法,可能适用或不适用于任何其他语法。您实际上只会在想要重复使用旧 POSIX 代码或 UNIX 脚本中的现有正则表达式时使用其他语法。

Boost 和正则表达式标准

Boost 文档喜欢谈论与 Perl 和 JavaScript 兼容,以及 boost::regex 如何在 C++11 中标准化为 std::regex。当我们将 Dinkumware 实作的 std::regex(包含在 Visual Studio 和 C++Builder 中)与 boost::regex 相比较时,我们发现类别和函数范本几乎相同。您的 C++ 编译器会很乐意编译使用 boost::regex 的代码,就像编译使用 std::regex 的相同代码一样。因此,本网站上 std::regex 主题 中提供的范例代码,如果将 std 替换为 boost,就能顺利与 Boost 搭配使用。

但是当您运行 C++ 应用程序时,Dinkumware 或 Boost 诠释您的正则表达式会产生很大的差异。尽管两者都提供相同的 六种语法,但这两个函数库的语法和行为并不相同。Boost 定义了 regex_constants::perl,这不是 C++11 标准的一部分。这实际上不是额外的语法,而只是 ECMAScript 和 JavaScript 的同义词。实际的 JavaScript 和实际的 Perl 所使用的正则表达式类型有很大的不同。因此,很明显地,将这些视为一种类型或语法的函数库无法与任何一种兼容。Boost 的 ECMAScript 语法是实际的 JavaScript 和 Perl 类型的交叉,并加入了一堆 Boost 特有的功能和特性。Dinkumware 的 ECMAScript 语法较接近实际的 JavaScript,但仍有显著的行为差异。Dinkumware 没有从 JavaScript 没有的功能中借用任何功能。

下表重点说明了 std::regex 和 Boost 中的 ECMAScript 语法与实际的 JavaScript 和 Perl 之间最重要的差异。有些是功能集的明显差异。但其他则是行为上的细微差异,可能会出乎意料地影响您。

功能 std::regex Boost JavaScript Perl
点符号符合换行符号 从未 缺省 从未 选项
锚点符合换行符号 始终 缺省 选项 选项
换行符号 CR、LF CR、LF、FF、NEL、LS、PSCR、LF、LS、PS LF
对非参与群组的反向参考符合空字符串自 1.47 版起失败符合空字符串失败
空字符类别 无法符合 不可能无法符合 不可能
自由间距模式 否 是 否 是
模式修改器 否 是 否 是
占有量词 否 是 否 是
命名捕获 否 .NET 语法 否 .NET 和 Python 语法
递归 否 原子 否 回溯
子常式 否 回溯 否 回溯
条件 否 是 否 是
原子组 否 是 否 是
原子组回溯捕获群组不适用 否 不适用 是
前缀和后缀边界 否 是 否 否
标准 POSIX 类别 是 是 否 是
单字母 POSIX 类别 否 是 否 否
功能 std::regex Boost JavaScript Perl
使用 Boost 的 C++ 正規表示式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » 使用 Boost 的 C++ 正規表示式

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

使用 Boost 的 C++ 正規表示式

Boost 是 C++ 的免費原始碼函式庫。在下載並解壓縮後,您需要執行 bootstrap 批次檔或指令碼,然後執行 b2 --with-regex 來編譯 Boost 的正規表示式函式庫。接著將您解壓縮 Boost 的資料夾新增至 C++ 編譯器的包含路徑中。將該資料夾的 stage\lib 子資料夾新增至連結器的函式庫路徑中。然後您可以在 C++ 程式碼中新增 #include <boost/regex.hpp> 來使用 Boost 正規表示式。

如果您使用 C++Builder,您應該從 Embarcadero 下載適用於您特定 C++Builder 版本的 Boost 函式庫。您取得的 Boost 版本取決於您的 C++Builder 版本,以及您鎖定的目標是 Win32 還是 Win64。XE3 到 XE8 中的 Win32 編譯器,以及 C++Builder 10 Seattle 到 10.1 Berlin 中的傳統 Win32 編譯器都停留在 Boost 1.39。XE3 到 XE6 中的 Win64 編譯器使用 Boost 1.50。XE7 到 10.1 Berlin 中的 Win64 編譯器使用 Boost 1.55。C++Builder 10 及更新版本中的新 C++11 Win32 編譯器使用與 Win64 編譯器相同的 Boost 版本。

本網站涵蓋 Boost 1.38、1.39 和 1.42 到最新的 1.73。Boost 1.40 引進許多新的正規表示式功能,這些功能取自 Perl 5.10。但它也引進了一些嚴重的錯誤,直到 Boost 1.42 才修正。因此我們完全忽略 Boost 1.40 和 1.41。我們仍涵蓋 Boost 1.38 和 1.39(它們具有相同的正規表示式功能),因為傳統的 Win32 C++Builder 編譯器停留在這個版本。如果您使用其他編譯器,您絕對應該使用 Boost 1.42 或更新版本,以避免現在已成為舊錯誤的問題。您最好使用 Boost 1.47 或更新版本,因為這個版本會變更涉及反向參照的特定行為,如果您稍後從 1.47 之前的版本升級到 1.47 之後的版本,可能會變更部分正規表示式的行為。

在實務上,您大多會使用 Boost 的 ECMAScript 語法。它是預設語法,提供的功能遠多於其他語法。每當本網站上的教學提到 Boost 但未提到任何語法時,寫下的內容就適用於 ECMAScript 語法,可能適用或不適用於任何其他語法。您實際上只會在想要重複使用舊 POSIX 程式碼或 UNIX 指令碼中的現有正規表示式時使用其他語法。

Boost 和正規表示式標準

Boost 文件喜歡談論與 Perl 和 JavaScript 相容,以及 boost::regex 如何在 C++11 中標準化為 std::regex。當我們將 Dinkumware 實作的 std::regex(包含在 Visual Studio 和 C++Builder 中)與 boost::regex 相比較時,我們發現類別和函式範本幾乎相同。您的 C++ 編譯器會很樂意編譯使用 boost::regex 的程式碼,就像編譯使用 std::regex 的相同程式碼一樣。因此,本網站上 std::regex 主題 中提供的範例程式碼,如果將 std 替換為 boost,就能順利與 Boost 搭配使用。

但是當您執行 C++ 應用程式時,Dinkumware 或 Boost 詮釋您的正規表示式會產生很大的差異。儘管兩者都提供相同的 六種語法,但這兩個函式庫的語法和行為並不相同。Boost 定義了 regex_constants::perl,這不是 C++11 標準的一部分。這實際上不是額外的語法,而只是 ECMAScript 和 JavaScript 的同義詞。實際的 JavaScript 和實際的 Perl 所使用的正規表示式類型有很大的不同。因此,很明顯地,將這些視為一種類型或語法的函式庫無法與任何一種相容。Boost 的 ECMAScript 語法是實際的 JavaScript 和 Perl 類型的交叉,並加入了一堆 Boost 特有的功能和特性。Dinkumware 的 ECMAScript 語法較接近實際的 JavaScript,但仍有顯著的行為差異。Dinkumware 沒有從 JavaScript 沒有的功能中借用任何功能。

下表重點說明了 std::regex 和 Boost 中的 ECMAScript 語法與實際的 JavaScript 和 Perl 之間最重要的差異。有些是功能集的明顯差異。但其他則是行為上的細微差異,可能會出乎意料地影響您。

功能 std::regex Boost JavaScript Perl
點符號符合換行符號 從未 預設 從未 選項
錨點符合換行符號 始終 預設 選項 選項
換行符號 CR、LF CR、LF、FF、NEL、LS、PSCR、LF、LS、PS LF
對非參與群組的反向參考符合空字串自 1.47 版起失敗符合空字串失敗
空字元類別 無法符合 不可能無法符合 不可能
自由間距模式 否 是 否 是
模式修改器 否 是 否 是
佔有量詞 否 是 否 是
命名捕獲 否 .NET 語法 否 .NET 和 Python 語法
遞迴 否 原子 否 回溯
子常式 否 回溯 否 回溯
條件 否 是 否 是
原子群組 否 是 否 是
原子群組回溯捕獲群組不適用 否 不適用 是
字首和字尾邊界 否 是 否 否
標準 POSIX 類別 是 是 否 是
單字母 POSIX 類別 否 是 否 否
功能 std::regex Boost JavaScript Perl
C++ Regular Expressions with Boost
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » C++ Regular Expressions with Boost

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

C++ Regular Expressions with Boost

Boost is a free source code library for C++. After downloading and unzipping, you need to run the bootstrap batch file or script and then run b2 --with-regex to compile Boost’s regex library. Then add the folder into which you unzipped Boost to the include path of your C++ compiler. Add the stage\lib subfolder of that folder to your linker’s library path. Then you can add #include <boost/regex.hpp> to your C++ code to make use of Boost regular expressions.

If you use C++Builder, you should download the Boost libraries for your specific version of C++Builder from Embarcadero. The version of Boost you get depends on your version of C++Builder and whether you’re targeting Win32 or Win64. The Win32 compiler in XE3 through XE8, and the classic Win32 compiler in C++Builder 10 Seattle through 10.1 Berlin are all stuck on Boost 1.39. The Win64 compiler in XE3 through XE6 uses Boost 1.50. The Win64 compiler in XE7 through 10.1 Berlin uses Boost 1.55. The new C++11 Win32 compiler in C++Builder 10 and later uses the same version of boost as the Win64 compiler.

This website covers Boost 1.38, 1.39, and 1.42 through the latest 1.73. Boost 1.40 introduced many new regex features borrowed from Perl 5.10. But it also introduced some serious bugs that weren’t fixed until Boost 1.42. So we completely ignore Boost 1.40 and 1.41. We still cover Boost 1.38 and 1.39 (which have identical regex features) because the classic Win32 C++Builder compiler is stuck on this version. If you’re using another compiler, you should definitely use Boost 1.42 or later to avoid what are now old bugs. You should preferably use Boost 1.47 or later as this version changes certain behaviors involving backreferences that may change how some of your regexes behave if you later upgrade from pre-1.47 to post-1.47.

In practice, you’ll mostly use the Boost’s ECMAScript grammar. It’s the default grammar and offers far more features that the other grammars. Whenever the tutorial on this website mentions Boost without mentioning any grammars then what is written applies to the ECMAScript grammar and may or may not apply to any of the other grammars. You’ll really only use the other grammars if you want to reuse existing regular expressions from old POSIX code or UNIX scripts.

Boost And Regex Standards

The Boost documentation likes to talk about being compatible with Perl and JavaScript and how boost::regex was standardized as std::regex in C++11. When we compare the Dinkumware implementation of std::regex (included with Visual Studio and C++Builder) with boost::regex, we find that the class and function templates are almost the same. Your C++ compiler will just as happily compile code using boost::regex as it does compiling the same code using std::regex. So all the code examples given in the std::regex topic on this website work just fine with Boost if you replace std with boost.

But when you run your C++ application then it can make a big difference whether it is Dinkumware or Boost that is interpreting your regular expressions. Though both offer the same six grammars, their syntax and behavior are not the same between the two libraries. Boost defines regex_constants::perl which is not part of the C++11 standard. This is not actually an additional grammar but simply a synonym to ECMAScript and JavaScript. There are major differences in the regex flavors used by actual JavaScript and actual Perl. So it’s obvious that a library treating these as one flavor or grammar can’t be compatible with either. Boost’s ECMAScript grammar is a cross between the actual JavaScript and Perl flavors, with a bunch of Boost-specific features and peculiarities thrown in. Dinkumware’s ECMAScript grammar is closer to actual JavaScript, but still has significant behavioral differences. Dinkumware didn’t borrow any features from Perl that JavaScript doesn’t have.

The table below highlights the most important differences between the ECMAScript grammars in std::regex and Boost and actual JavaScript and Perl. Some are obvious differences in feature sets. But others are subtle differences in behavior that may bite you unexpectedly.

Feature std::regex Boost JavaScript Perl
Dot matches line breaks never default never option
Anchors match at line breaks always default option option
Line break characters CR, LF CR, LF, FF, NEL, LS, PSCR, LF, LS, PS LF
Backreferences to non-participating groupsMatch empty stringfail since 1.47Match empty stringfail
Empty character class Fails to match Not possibleFails to match Not possible
Free-spacing mode no YES no YES
Mode modifiers no YES no YES
Possessive quantifiers no YES no YES
Named capture no .NET syntax no .NET & Python syntax
Recursion no atomic no backtracking
Subroutines no backtracking no backtracking
Conditionals no YES no YES
Atomic groups no YES no YES
Atomic groups backtrack capturing groupsn/a no n/a YES
Start and end of word boundaries no YES no no
Standard POSIX classes YES YES no YES
Single letter POSIX classes no YES no no
Feature std::regex Boost JavaScript Perl
©2015-2025 艾丽卡 support@alaica.com