相关文章推荐
阳光的牙膏  ·  图书馆-大连海事大学本科招生网·  8 月前    · 
买醉的铁链  ·  Developer Community·  9 月前    · 
性感的凉面  ·  无源光网络(PON)与有源光网络(AON)有 ...·  9 月前    · 
旅途中的牛腩  ·  Songbird: Ultimate ...·  1 年前    · 
大方的羽毛球  ·  【科普】新冠感染后为什么会排尿异常?-健康教 ...·  1 年前    · 
Code  ›  C++正则表达式校验某个字符串是否是合格的email开发者社区
c++ 正则表达式 email
https://cloud.tencent.com/developer/article/2304688
谦逊的茴香
2 年前
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;
 
推荐文章
阳光的牙膏  ·  图书馆-大连海事大学本科招生网
8 月前
买醉的铁链  ·  Developer Community
9 月前
性感的凉面  ·  无源光网络(PON)与有源光网络(AON)有什么区别?
9 月前
旅途中的牛腩  ·  Songbird: Ultimate Collection : Kenny G: Amazon.in: Books
1 年前
大方的羽毛球  ·  【科普】新冠感染后为什么会排尿异常?-健康教育-清华大学附属北京清华长庚医院泌尿外科
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号