第十七章 17.3.1节练习

练习17.14

编写几个正则表达式,分别触发不同错误。运行你的程序,观察编译器对每个错误的输出。

解答:

这个就不一一列举出来了。如果正则表达式写错,在运行时程序会在编译表达式的时候直接崩溃。


练习17.15

编写程序,使用模式查找违反“i在e之前,除非在c之后”规则的单词。你的程序应该提示用户输入一个单词,然后掷出此单词是够符合要求。用一些违反和未违反规则的单词测试你的程序。

解答:

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

using namespace std;

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

	string test_str;

	while (cout << "\nPlease enter a word for test:" << endl, cin >> test_str){
		if (regex_search(test_str, results, r))
			cout << "this word fit the requirements." << endl;
		else
			cout << "this word don't fit the requirements." << endl;
	}
}

练习17.16

如果前一题程序中的regex对象用“[^c]ei"进行初始化,将会发生什么?用词模式测试你的程序,检查你的答案是否正确。

解答:

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

using namespace std;

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

	string test_str;

	while (cout << "\nPlease enter a word for test:" << endl, cin >> test_str){
		if (regex_search(test_str, results, r))
			cout << "this word fit the requirements." << endl;
		else
			cout << "this word don't fit the requirements." << endl;
	}
}
在VS2013中并没有什么影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值