在C++ STL中的结果

这个 匹配结果::空() 是C++中的内置函数,返回 符合事实的 如果smatch对象不包含匹配项。 语法:

null
smatch_name.empty()Note: smatch_name is an object of match_results class.

参数: 此函数不接受任何参数。 返回值: 当对象是默认构造的时,此函数返回true;如果任何正则表达式匹配或正则表达式搜索找到至少一个匹配,则返回false。 错误和例外:

  • 它也不例外。
  • 传递参数时引发异常。

注: 第一个元素总是包含整个正则表达式匹配项,而其他元素则包含特定的正则表达式匹配项 捕获群 . 下面的程序说明了上述方法。 项目1:

CPP

// CPP program to illustrate
// match_results empty() in C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s( "harsha" );
regex re1( "Geeks.*" );
regex re2( "geeks.*" );
smatch match1, match2;
regex_match(s, match1, re1);
regex_match(s, match2, re2);
// if match1 is empty it returns
// true or else false
if (match1.empty()) {
cout << "Regex-1 did not match" << endl;
}
else {
cout << "Regex-1 matched" << endl;
}
// if match2 is empty it returns
// true or else false
if (match2.empty()) {
cout << "Regex-2 did not match" << endl;
}
else {
cout << "Regex-2 matched" << endl;
}
return 0;
}


输出:

Regex-1 did not matchRegex-2 did not match

项目2:

CPP

// CPP program to illustrate
// match_results empty() in C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s( "geeksforgeeks" );
regex re1( "Geeks.*" );
regex re2( "geeks.*" );
smatch match1, match2;
regex_match(s, match1, re1);
regex_match(s, match2, re2);
// if match1 is empty it returns
// true or else false
if (match1.empty()) {
cout << "Regex-1 did not match" << endl;
}
else {
cout << "Regex-1 matched" << endl;
}
// if match2 is empty it returns
// true or else false
if (match2.empty()) {
cout << "Regex-2 did not match" << endl;
}
else {
cout << "Regex-2 matched" << endl;
}
return 0;
}


输出:

Regex-1 did not matchRegex-2 matched

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享