Regex 头文件: "boost/regex.hpp" 正则表达式被封装为一个类型 basic_regex 的对象。我们将在下一节更深入地讨论正则表达式如何被编译和分析,这里我们首先粗略地看看 basic_regex ,以及这个库中三个最重要的算法。 namespace boost { template > 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 regex; typedef basic_regex 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 regex; typedef basic_regex wregex; 不要使用类型 basic_regex 来定义变量,你应该使用这两个typedef 中的一个。这两个类型,regex 和 wregex, 是两种字符类型的缩写,就如 string 和 wstring 是 basic_string 和 basic_string的缩写一样。这种相似性是不一样的,某种程度上,regex 是一个特定类型的字符串的容器。 普通函数 template bool regex_match( const cha...