天天看点

第十七章 17.3.2节练习

练习17.17

更新你的程序,令他查找输入序列中所有违反“ei”语法规则的单词。

解答:

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main(){
	string pattern("c[ei]");
	pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
	regex r(pattern, regex::icase);
	smatch results;

	string test_str = "receipt freind theif receive";

	for (sregex_iterator it(test_str.begin(), test_str.end(), r), end_it; it != end_it; ++it){
		cout << it->str();
		cout	<< endl;
	}
}
           

尝试了几种smatch的操作,感觉还是很麻烦,索性就直接改正则表达式来完成。

练习17.18

修改你的程序,忽略包含“ei”但非拼写错误的单词,如“albeit”和“neighbor”。

解答:

17题的程序能处理这种情况。

继续阅读