头文件—regex
组件 | 作用 |
---|---|
regex | 表示有一个正则表达式的类 |
regex_match | 将一个字符序列与一个正则表达式匹配 |
regex_search | 寻找第一个与正则表达式匹配的子序列 |
regex_replcace | 使用给定的格式匹配一个正则表达式 |
sregex_iterator | 迭代器适配器,调用 regex_search 遍历 string 中所有匹配的子串 |
smatch | 容器类,保存在 string 中搜索的结果 |
ssub_match | string 中匹配的子表达式结果 |
regex_search & regex_match 参数
- #include < iostream > #include < regex > using namespace std;
- int main() {
- string pattern("[^c]ei");
- pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
- regex r(pattern);
- smatch result;
- string test_str = "receipt freind theif receive";
- if (regex_search(test_str, result, r)) cout <
用法: regex r(re),regex r(re,f), r1 = re, r1.assign(re,f)
flag | function |
---|---|
icase | 忽略大小写 |
nosubs | 不保存匹配的子表达式 |
optimize | 执行速度优先于构造速度 |
ECMAScript | 使用 ECMA-262 指定语法 |
basic | 使用 posix 基本的正则表达式语法 |
extended | 使用 posix 扩展的正则表达式语法 |
awk | 使用 posix 版本的 awk 语言的语法 |
grep | 使用 posix 版本的 grep 语法 |
egrep | 使用 posix 版本的 egrep 语法 |
- #include < iostream > #include < regex > using namespace std;
- int main() {
- //注意下面到反斜杠
- regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex: :icase);
- smatch results;
- string filename;
- while (cin >> filename) {
- if (regex_search(filename, results, r)) cout <
如果我们编写的正则表达式存在错误,则运行时会抛出一个类型为 regex_error 的异常,regex_error 有一个 what 操作描述发生了什么错误,还有一个 code 成员返回某个错误类型对应的编码。
regex 类保存类型 char 的正则表达式,wregex 类保存类型为 wchar_t
类型差异不仅在于字符类型,还在于序列是在 string 中还是在数组中
- smatch : 表示 string 类型序列
- cmatch : 表示字符数组序列
- wsmatch: 表示宽字符串(string)序列
- wcmatch: 表示宽字符数组
note: RE 库类型必须与输入序列类型匹配
序列类型 | 正则表达式类 |
---|---|
string | regex、smatch、ssub_match、sregex_iterator |
const char * | regex、cmatch、csub_match、cregex_iterator |
wstring | wregex、wsmatch、wssub_match、wsregex_iterator |
const wchar_t * | wregex、wcmatch、wcsub_match、wcregex_iterator |
sregex_iterator 操作:
操作 | 作用 |
---|---|
sregex_iterator it(b,e,r) | 一个 sregex_iterator 迭代器,遍历 b 和 e 表示的 string 并将 it 定位到第一个匹配的位置 |
sregex_iterator end | 尾后迭代器 |
*it(it->) | 根据最后一个调用 regex_search 的结果,返回一个 smatch 的对象引用或指向 smatch 的指针 |
it++ | 从序列当前匹配位置开始调用 regex_search,返回递增后的迭代器 |
it++ | 从序列当前匹配位置开始调用 regex_search,返回递增前的迭代器 |
it1 == it2(it1 != it2) | 如果两个都是尾后迭代器则相等,非尾后迭代器从相同到序列和 regex 构造则相等 |
- #include < iostream > #include < regex > using namespace std;
- int main() {
- string pattern("[^c]ei");
- pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
- regex r(pattern);
- smatch result;
- string test_str = "receipt freind theif receive";
- /* regex_search(test_str,result,r);
- for(smatch::iterator it = result.begin(); it != result.end(); ++it)
- {
- cout<<it->str()<for(sregex_iterator it(test_str.begin(),test_str.end(),r),end_it; it != end_it; it++)
- {
- cout<<it->str()<*/
匹配类型有名为 prefix 和 suffix 的成员,分别表示分别表示输入序列中当前的匹配之前和之后部分的 ssub_match 对象。一个 ssub_match 对象有 str 和 length 的成员,返回匹配的 string 和 string 的长度。
- #include < iostream > #include < regex > using namespace std;
- int main() {
- string pattern("[^c]ei");
- pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
- regex r(pattern);
- smatch result;
- string test_str = "receipt freind theif receive";
- for (sregex_iterator it(test_str.begin(), test_str.end(), r), end_it; it != end_it; it++) {
- auto pos = it - >prefix().length();
- pos = pos > 40 ? pos - 40 : 0;
- cout << it - >prefix().str().substr(pos) << "\n\t\t>>> " << it - >str() << " <<<\n" << it - >suffix().str().substr(0, 40) <
操作 | 作用 |
---|---|
m.ready() | 如果调用 regex_search 或 regex_match 则返回 true 否则返回 false |
m.size() | 如果失败返回 0,否则返回最近一次匹配的正则表达式中子表达式的数目 |
m.empty() | 若 m.size() 为 0,则返回 true |
m.prefix() | 一个 ssub_match 对象,表示当前匹配之前的序列 |
m.suffix() | 一个 ssub_match 对象,表示当前匹配之后的序列 |
m.length(n) | 第 n 个子表达式的大小默认为 0 |
m.position(n) | 第 n 个子表达式距序列开始的距离 |
m.str(n) | 第 n 个子表达式匹配的 string |
m.begin(),m.end() | m 中元素范围迭代器 |
m.cbegin(),m.cend() | 常量迭代器 |
正则表达式通常包含一个或多个子表达式。正则表达式通常用括号表示子表达式。
- regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$", regex: :icase);
- #include < iostream > #include < regex > using namespace std;
- int main() {
- regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$", regex: :icase);
- smatch results;
- string filename;
- while (cin >> filename) {
- if (regex_search(filename, results, r)) cout < 1) < //str中参数n表示第n个子表达式,默认为0,表示整个匹配
- }
- }
子匹配操作 | 作用 |
---|---|
matched | 一个 public bool 成员,指出此 ssub_match 是否匹配了 |
first,second | public 数据成员,指向匹配序列的首元素和尾后迭代器 |
length() | 匹配的大小,如果 matched 为 false 返回 0 |
str() | 返回一个包含输入中匹配部分的 string,如果 matched 为 false,返回空 string |
s=ssub | 将 ssub_match 对象 ssub 转化为 string 对象 s。等价于 s=ssub.str() |
- #include < iostream > #include < regex > using namespace std;
- bool valid(const smatch & m) {
- if (m[1].matched) return m[3].matched && (m[4].matched == false || m[4].str() == " ");
- else return ! m[3].matched && m[4].str() == m[6].str();
- }
- int main() {
- //左"(",三位数字,右")","."或"-"或" ",三位数字,"或"-"或" ",四位数字
- //"?"表示可有可无
- string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
- regex r(phone);
- string s;
- while (getline(cin, s)) {
- for (sregex_iterator it(s.begin(), s.end(), r), end_it; it != end_it; it++) {
- if (valid( * it)) cout << "valid:" < str() <
- else cout << "invalid:" < str() <
- return 0;
- }
当我们希望在输入序列中查找并替换一个正则表达式时可以调用 regex_replace
替换操作 | 作用 |
---|---|
m.format(dest,fmt,mft) | 使用格式化输出,匹配在 m 中,可选的 match_flag_type 在 mft 中。写入迭代器 dest 指向的目的位置,fmt 可以是一个 string,也可以是表示字符数组中范围的一对指针 |
m.format(fmt,mft) | 同上,返回一个 string,fmt 可以是一个 string,也可以是指向空字符结尾的字符数组指针 |
regex_replace(dest,seq,r,fmt,mft) | 遍历 seq,用 regex_search 查找与 r 匹配的子串。使用格式字符串 fmt 和可选的 match_flag_type 来标志生成输出,将输出写入迭代器 dest 指向的位置,并接受一对迭代器 seq 表示范围 |
regex_replace(seq,r,fmt,mft) | 同上,返回一个 string,seq 既可以是一个 string,也可以是一个指向空字符结尾的字符数组指针 |
- #include < iostream > #include < fstream > #include < regex > using namespace std;
- int main() {
- ifstream ifs;
- string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
- string fmt = "$2.$5.$7";
- regex r(phone);
- ifs.open("modify" > );
- if (!ifs) {
- cout << "modify" > <
- return - 1;
- }
- string line;
- while (getline(ifs, line)) {
- cout <
- return 0;
- }
匹配标志 | 作用 |
---|---|
match_default | 同 format_default |
match_not_bol | 不将首字符作为行首处理 |
match_not_eol | 不将尾字符作为行尾处理 |
match_not_bow | 不将首字符作为单词首处理 |
match_not_eow | 不将尾字符作为单词尾处理 |
match_any | 如果存在多个匹配,则可返回任意一个匹配 |
match_not_null | 不匹配任何空序列 |
match_continuous | 匹配必须从输入到首字符开始 |
match_prev_avail | 输入序列包含第一个匹配之前的内容 |
format_default | 使用 ECMAScript 规则替换字符串 |
format_sed | 用 posix sed 规则替换字符串 |
format_no_copy | 不输出输入序列中未匹配到部分 |
format_first_only | 只替换子表达式的第一次出现 |
来源: http://lib.csdn.net/article/cplusplus/48958