相关文章推荐
气宇轩昂的香菇  ·  如何在Presto中将整数转换为字符串?·  1 年前    · 
曾经爱过的蜡烛  ·  c# - How to implement ...·  2 年前    · 
打盹的板栗  ·  我校学子在第8届中国大学生计算机设计大赛中取 ...·  2 年前    · 
Code  ›  C++正则表达式校验某个字符串是否是合格的email开发者社区
c++ 正则表达式 email
https://cloud.tencent.com/developer/article/2304688
谦逊的茴香
1 年前
ccf19881030

C++正则表达式校验某个字符串是否是合格的email

腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
ccf19881030
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > C++正则表达式校验某个字符串是否是合格的email

C++正则表达式校验某个字符串是否是合格的email

作者头像
ccf19881030
发布 于 2023-07-24 19:45:10
201 0
发布 于 2023-07-24 19:45:10
举报
文章被收录于专栏: ccf19881030的博客 ccf19881030的博客

C++正则表达式校验某个字符串是否是合格的email

可以借助正则表达式校验某个字符串是否是合规的电子邮箱。对于邮箱的正则表达式有严格的模式,如: ^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$ 对应的C++实现如下: // EmailValidator.h

#ifndef EMAIL_VALIDATOR_H
#define EMAIL_VALIDATOR_H
#include <string>
// Email地址校验类
class EmailValidator
public:
	// 校验email是否是合法的email地址
	static bool isValidEmail(const std::string& email);
#endif	// EMAIL_VALIDATOR_H

// EmailValidator.cpp

#include "EmailValidator.h"
#include <regex>
namespace {
	const std::string EMAIL_PATTERN = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*" \
		"@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
bool EmailValidator::isValidEmail(const std::string& email)
	std::regex pattern(EMAIL_PATTERN);
	// Tests whether a regular expression matches the entire target string.
	if (std::regex_match(email, pattern)) {
		return true;
	return false;
}

//测试main.cpp

#include "EmailValidator.h"
#include <iostream>
using namespace std;
// C++11标准 STL正则表达式 验证电子邮件地址
// https://blog.csdn.net/kyfvc/article/details/41082025
int main(int argc, char* argv[])
	std::string emailAddress;
	// while (std::getline(cin, emailAddress))
	while (cin >> emailAddress)
		if (EmailValidator::isValidEmail(emailAddress)) {
			std::cout << "您输入的电子邮件地址: " << emailAddress << " 合法" << std::endl;
		} else {
			std::cout << "您输入的电子邮件地址: " << emailAddress << " 不合法" << std::endl;
 
推荐文章
气宇轩昂的香菇  ·  如何在Presto中将整数转换为字符串?
1 年前
曾经爱过的蜡烛  ·  c# - How to implement ICommand without parameters - Stack Overflow
2 年前
打盹的板栗  ·  我校学子在第8届中国大学生计算机设计大赛中取得优异成绩
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号