## Regex
### 頭文件: `"boost/regex.hpp"`
正則表達式被封裝為一個類型 `basic_regex`的對象。我們將在下一節更深入地討論正則表達式如何被編譯和分析,這里我們首先粗略地看看 `basic_regex` ,以及這個庫中三個最重要的算法。
```
namespace boost {
template <class charT,
class traits=regex_traits<charT> >
class basic_regex {
public:
explicit basic_regex(
const charT* p,
flag_type f=regex_constants::normal);
bool empty() const;
unsigned mark_count() const;
flag_type flags() const;
};
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
}
```
### 成員函數
```
explicit basic_regex (
const charT* p,
flag_type f=regex_constants::normal);
```
這個構造函數接受一個包含正則表達式的字符序列,還有一個參數用于指定使用正則表達式時的選項,例如是否忽略大小寫。如果`p`中的正則表達式無效,則拋出一個 `bad_expression` 或 `regex_error` 的異常。注意這兩個異常其實是同一個東西;在寫這本書之時,尚未改變當前使用的名字 `bad_expression` ,但下一個版本的Boost.Regex將會使用 `regex_error`.
```
bool empty() const;
```
這個成員函數是一個謂詞,當`basic_regex`實例沒有包含一個有效的正則表達式時返回 `true` ,即它被賦予一個空的字符序列時。
```
unsigned mark_count() const;
```
`mark_count` 返回`regex`中帶標記子表達式的數量。帶標記子表達式是指正則表達式中用圓括號括起來的部分。匹配這個子表達式的文本可以通過調用某個正則表達式算法而獲得。
```
flag_type flags() const;
```
返回一個位掩碼,其中包含這個`basic_regex`所設置的選項標志。例如標志 `icase`, 表示正則表達式忽略大小寫,標志 `JavaScript`, 表示regex使用JavaScript的語法。
```
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
```
不要使用類型 `basic_regex`來定義變量,你應該使用這兩個`typedef`中的一個。這兩個類型,`regex` 和 `wregex`, 是兩種字符類型的縮寫,就如 `string` 和 `wstring` 是 `basic_string<char>` 和 `basic_string<wchar_t>`的縮寫一樣。這種相似性是不一樣的,某種程度上,`regex` 是一個特定類型的字符串的容器。
### 普通函數
```
template <class charT,class Allocator,class traits >
bool regex_match(
const charT* str,
match_results<const charT*,Allocator>& m,
const basic_regex<charT,traits >& e,
match_flag_type flags = match_default);
```
`regex_match` 判斷一個正則表達式(參數 `e`)是否匹配整個字符序列 `str`. 它主要用于驗證文本。注意,這個正則表達式必須匹配被分析串的全部,否則函數返回 `false`. 如果整個序列被成功匹配,`regex_match` 返回 `True`.
```
template <class charT,class Allocator, class traits>
bool regex_search(
const charT* str,
match_results<const charT*,Allocator>& m,
const basic_regex<charT,traits >& e,
match_flag_type flags = match_default);
```
`regex_search` 類似于 `regex_match`, 但它不要求整個字符序列完全匹配。你可以用 `regex_search` 來查找輸入中的一個子序列,該子序列匹配正則表達式 `e`.
```
template <class traits,class charT>
basic_string<charT> regex_replace(
const basic_string<charT>& s,
const basic_regex<charT,traits >& e,
const basic_string<charT>& fmt,
match_flag_type flags = match_default);
```
`regex_replace` 在整個字符序列中查找正則表達式`e`的所有匹配。這個算法每次成功匹配后,就根據參數`fmt`對匹配字符串進行格式化。缺省情況下,不匹配的文本不會被修改,即文本會被輸出但沒有改變。
這三個算法都有幾個不同的重載形式:一個接受 `const charT*` (`charT` 為字符類型), 另一個接受 `const basic_string<charT>&`, 還有一個重載接受兩個雙向迭代器作為輸入參數。
- 序
- 前言
- Acknowledgments
- 關于作者
- 本書的組織結構
- Boost的介紹
- 字符串及文本處理
- 數 據結構, 容器, 迭代器, 和算法
- 函數對象及高級編程
- 泛 型編程與模板元編程
- 數學及數字處理
- 輸入/輸出
- 雜項
- Part I: 通用庫
- Library 1. Smart_ptr
- Smart_ptr庫如何改進你的程序?
- 何時我們需要智能指針?
- Smart_ptr如何適應標準庫?
- scoped_ptr
- scoped_array
- shared_ptr
- shared_array
- intrusive_ptr
- weak_ptr
- Smart_ptr總結
- Library 2. Conversion
- Conversion 庫如何改進你的程序?
- polymorphic_cast
- polymorphic_downcast
- numeric_cast
- lexical_cast
- Conversion 總結
- Library 3. Utility
- Utility 庫如何改進你的程序?
- BOOST_STATIC_ASSERT
- checked_delete
- noncopyable
- addressof
- enable_if
- Utility 總結
- Library 4. Operators
- Operators庫如何改進你的程序?
- Operators
- 用法
- Operators 總結
- Library 5. Regex
- Regex庫如何改進你的程序?
- Regex 如何適用于標準庫?
- Regex
- 用法
- Regex 總結
- Part II: 容器及數據結構
- Library 6. Any
- Any 庫如何改進你的程序?
- Any 如何適用于標準庫?
- Any
- 用法
- Any 總結
- Library 7. Variant
- Variant 庫如何改進你的程序?
- Variant 如何適用于標準庫?
- Variant
- 用法
- Variant 總結
- Library 8. Tuple
- Tuple 庫如何改進你的程序?
- Tuple 庫如何適用于標準庫?
- Tuple
- 用法
- Tuple 總結
- Part III: 函數對象與高級編程
- Library 9. Bind
- Bind 庫如何改進你的程序?
- Bind 如何適用于標準庫?
- Bind
- 用法
- Bind 總結
- Library 10. Lambda
- Lambda 庫如何改進你的程序?
- Lambda 如何適用于標準庫?
- Lambda
- 用法
- Lambda 總結
- Library 11. Function
- Function 庫如何改進你的程序?
- Function 如何適用于標準庫?
- Function
- 用 法
- Function 總結
- Library 12. Signals
- Signals 庫如何改進你的程序?
- Signals 如何適用于標準庫?
- Signals
- 用法
- Signals 總結