C++正则表达式常见使用

百度上一搜就可以搜到很详细的相关正则表达式,以下内容用到了邮箱的正则表达式以及一些很基础的正则表达式。

表达式的多少不是问题,主要是掌握如何使用。
需要头文件#include< regex >
主要围绕正则表达式的三个常用点:
1:匹配 regex_match(string str,regex reg)
2:捕获 bool regex_search(string str,smatch result,regex reg);
3:替换 string regex_replace(str,oldStr,regex reg,string replaceStr,int flag);

#include<regex>
#include<iostream>
using namespace std;
//1.匹配  regex_match(string str,regex reg);
//匹配返回值true。不匹配返回false
void testMatch() {


	//构造regex对象
	//1.2调用regex_match
#if 0
	regex reg("^\\w+([-+.]\\w+)*@\\w+([-.]\\w +)*\\.\\w+([-.]\\w +)*$");
	while (1) {
		string str;
		cin >> str;
		int result = regex_match(str,reg);
		if (result) {
			cout << "输入正确!\n";
			break;
		}
	}
#endif 
	//正则匹配一定是完全匹配
	string str = "Ni666";
	/*regex strRegex("[a-z0-9]+");
	if (regex_match(str, strRegex)) {
		cout << "匹配!\n";
	}
	else {
		cout << "不匹配!\b";
	}*/
	//正则匹配的时候,可以忽略大写
	regex regexStr("[a-z0-9]+", regex_constants::icase);
	if (regex_match(str, regexStr)) {
		cout << "匹配!\n";
	}
	else {
		cout << "不匹配!\b";
	}
	//字符串可以直接充当正则表达式,但必须要完全一样,才是匹配
	regex strRule("Ni666");
	if (regex_match(str, strRule)) {
		cout << "匹配\n";
	}
	else {
		cout << "不匹配\n";
	}
}
//2.捕获(截取或者处理字符串)
void testGetString() {
	string str = "Nihao1314laobiao66";
	/*
		bool regex_search(string str,smatch result,regex reg);
	*/
	smatch result;
	//正常使用,只做一次成功匹配
	bool flag = regex_search(str, result, regex("\\d+"));
	if(flag){
		cout << "匹配成功:" << result.str() << endl;
		cout << "前缀字符串为:" << result.prefix() << endl;
		cout << "后缀字符串为:" << result.suffix() << endl;
	}
	//通过迭代器实现所有相关匹配。
	regex rule("\\d+");
	sregex_iterator pos(str.begin(), str.end(), rule);
	sregex_iterator end;
	while (pos != end) {
		cout << pos->str();
		pos++;
	}
	cout << endl;
	
	//按照正则做字符串拆解
	/*
		sreget_token_iterator(iterator begin,iterator end, regex reg,int flag)'
		flag值:
		 0:所有匹配到的
		-1:所有不匹配的。
	*/
	sregex_token_iterator tokenBegin(str.begin(), str.end(), rule,0);
	sregex_token_iterator tokenEnd;
	while (tokenBegin != tokenEnd) {
		cout << tokenBegin->str() << '\t';
		tokenBegin++;
	}
	cout << endl;
}
// 替换
void testReplace() {

	/*
		string regex_replace(str,oldStr,regex reg,string replaceStr,int flag);
	*/
	string str = "ILoveyou13IMissyou14me";
	regex reg("\\d+");
	cout << "8888" << regex_replace(str, reg, "8888") << endl;
	cout << "原str:" << str << endl;//源字符串没有改变
	//如何控制替换
	//1.只替换第一次匹配到的
	cout << "only_first:" <<
	regex_replace(str, reg, "8888", regex_constants::format_first_only)
	<< endl;
	//2.不拷贝不匹配的
	cout << "no_copy:" <<
	regex_replace(str, reg, "8888", regex_constants::format_no_copy)
	<< endl;
	//3.默认方式:
	cout << "no_copy:" <<
	regex_replace(str, reg, "8888", regex_constants::format_default)
	<< endl;
}
int main() {

	testMatch();
	testGetString();
	testReplace();
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值