- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检测无效输入,其中变量 n
不应包含任何符号:;:"'[]*^%$#@!
,在regex r
中定义,代码如下:
#include "iostream"
#include "string"
#include "sstream"
#include "regex"
using namespace std;
struct Person{
// constructor
Person(string n, int a)
: name(n), age(a) {
if (a <= 0 || a > 150) throw std::out_of_range("Age out of range.");
// regex r(";:\"\'[]*^%$#@!");
// regex r("\:|\;|\"|\'|\[|\]|\*|\^|\%|\$|\#|\@|\!");
// regex r("/[\:\;\"\'\[\]\*\^\%\$\#\@\!]/");
// regex r("/[;:\"\'[]*^%$#@!]/");
smatch matches;
regex_match(n, matches ,r);
if (!matches.empty()) throw std::invalid_argument("Name contains invalid symbols.");
}
// data members
string name;
int age;
};
//-----------------------------------------------------------------------------------------
int main(){
try{
vector<Person> people;
string input_termination = "end";
while(true){
cout <<"Type name and age; terminate with \"end\":\n>>";
string line;
getline(cin, line);
stringstream ss(line);
string n;
int a;
ss >> n >> a;
if (n == input_termination) break;
else people.emplace_back(Person(n,a));
}
cout <<"\nStored people: \n";
for (auto it = people.begin(); it != people.end(); ++it) cout << *it <<'\n';
} catch (exception& e){
cerr << e.what() << endl;
getchar();
} catch (...){
cerr <<"Exception!" << endl;
getchar();
}
}
注释行是所有不成功的尝试,它们要么导致没有throw
1,要么导致以下错误消息:
regular expression error
如何在上述构造函数中正确定义和使用 regex
,以便在 n
包含任何禁止符号时检测到它?
注意:我已阅读建议的来源。
1。当使用包含某些符号的无效名称来初始化对象时。
最佳答案
主要问题是某些特殊字符需要使用 \
字符进行转义,以便正则表达式引擎将其读取为自身(即 *
是一个特殊的标记,意思是匹配 0 个或多个先前的标记)。这意味着您不仅需要转义通常的 ' && "
字符,还需要为其他字符添加 \
字符前缀
你可以通过以下模式实现你想要的:
";:\\\"\\\'\[\]\*\^%\$#@!"
关于c++ - 使用正则表达式检查输入有效性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33488675/
网站的日常维护通常涉及验证链接是否有效、标记不良链接等。 我知道如何通过脚本匹配电子邮件地址(尤其是在它们位于 mailto: 链接中的网页上下文中)。我的问题是如何在不向地址发送垃圾邮件的情况下验证
我正在尝试将大型 UInt16 数组保存到文件中。 positionCnt大约是50000,stationCnt大约是2500。直接保存,没有GZipStream,文件大约250MB,可以通过外部zi
我正在寻找表格方面的 HTML5 规范。我正在做一个有很多数据表的网络应用程序。 这些表通常有一个数据库行映射到一个表行。一切都很好。然而,一个特定的问题 child 有太多的列,需要将其分布在表中的
我目前正在研究用于客户端目的的Azure流媒体服务,我想知道是否有一种方法可以为自适应流媒体创建时间有限的URL(出于安全目的,我们需要这个,当用户想要流媒体时,我们会验证权限)创建限时 URL)。我
我目前正在研究用于客户端目的的Azure流媒体服务,我想知道是否有一种方法可以为自适应流媒体创建时间有限的URL(出于安全目的,我们需要这个,当用户想要流媒体时,我们会验证权限)创建限时 URL)。我
我想做的是测试我编写的一些 Lucene 代码,并且想要一些有关使用 JUnit 进行测试时的最佳实践的信息。顺便说一句,Lucene 是一个搜索引擎,您可以使用它创建一个平面文件来索引一堆数据。 所
我有一个 .net 核心应用程序,我在注册后创建了一个电子邮件 token ,然后通过电子邮件发送它。这个想法是,用户可以在客户端应用程序中使用该电子邮件 token ,我想检查它的有效性(如果它过期
我的印象是使用 javascript 经常破坏 W3C xhtml/css 有效性。这是真的?如果是这样,为什么我可以在我的网页中使用 javascript 并且仍然使它们符合 W3C 标准(html
我简直是在绞尽脑汁想弄明白这个问题。给定一个无向连通图 G , G 中的所有边都具有未知成本,但已知每条边的每个成本的区间,例如边 e 的成本在闭区间 [i,j] 中,其中 i 和 j 是实数。我还得
检查 URL 有效性的一种直接方法是简单地处理 MalformedURLException 异常: try { URL base = new URL(SomeString); } catch (M
如何在 shell 脚本中检查 0.0.0.0 到 255.255.255.255 范围内的 IP 地址的有效性? 最佳答案 如果您使用的是 bash,则可以对模式进行简单的正则表达式匹配,而无需验证
我已成功通过我的 Angular 应用程序中的 Azure AD ( https://login.microsoftonline.com/ {tenantId}/oauth2/v2.0/authori
我已经获得了文件:一个 .der- 和一个 .p12-文件,以及一个 key 对,我们应该用它来在 Java 代码中创建和验证数字签名。 我必须将两个 key 保存在 MySql-Database-T
我一直在关注 this使用 lumen 和 vuejs 进行身份验证的教程。我正在使用 this库(tymon/jwt-auth)。 一切正常。我的 API 为我的数据处理所有事情,并为我的前端框架提
我想知道,但是引用交易必须在过去 730 天内发生,因为账单协议(protocol) ID 可能在两年后不可用。因此,如果第一笔交易在到期前完成,可以说是在 600 天之后,那么 ID 将在 130
std::string.npos 曾经有效吗? (与正确的 std::string::npos 相反。) 我在我正在处理的一个旧项目中经常看到它,它不能用 VS2010 编译。 它是前标准时代的东西吗
我正在使用 Passport 来验证用户进入我的应用程序 我已经为此创建了一个通行证策略 passport.use(new GoogleStrategy({ clientID: process
我正在使用来自 API 的 URL,其中搜索词用引号括起来。例如:http://www.example.com/searchterm="搜索"。 但是,由于引号,我的 NSURL(由 URLWithS
有谁知道 GitLab 上 OAuth 访问 token 的有效期是多少? 至少需要 12 小时(根据经验),但我想确定一下,所以我不会不必要地刷新 token 。 PS:亚搏体育app ...如果在
使用 FirebaseStorage 的 getDownloadUrl 时,我对 url(&token=) 中的“ token ”有疑问。 https://firebasestorage.google
我是一名优秀的程序员,十分优秀!