Bootstrap

C++ 正则表达式

正则表达式:

又称为: 规则表达式。
作用: 一般用于 检索 或者替换某些符合所选设置规则(或模式)的文本
使用: 正则表达式是 普通字符或者一些特殊字符组成的字符模式,将此作为一个字符模板进行与所需要的文本进行字符串的模糊匹配,以达到所需要的效果

正则表达式常用字符:

在这里插入图片描述在这里插入图片描述

头文件: #include

三种用法:
regex_match :全文匹配,即要求整个字符串符合匹配规则,返回true或false(不改变字符串本身)
regex_search:搜索匹配,即搜索字符串中存在符合规则的子字符串。
regex_replace: 替换匹配,即可以将符合匹配规则的子字符串替换为其他字符串。(会改变字符串本身)

测试代码:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <regex>
#include <iostream>
#include <string>
using namespace std;


int main1()
{
   
	string n;
	int res = 0;
	while (!res)
	{
   
		cout << "请输入手机号" << endl;
		cin >> n;
		
		res = regex_match(n, regex("132\\d{8}"));
		cout << res << endl;
		if (res == 0
;