我有一个字符串“\ 03 COUNTER \ TIME_NOW”
COUNTER前缀允许的最小有效范围是01到09
示例:
“\ 02COUNTER \ TIME_NOW”:有效
“\ 10COUNTER \ TIME_NOW”:无效
“\ 00COUNTER \ TIME_NOW”:无效
“\ 88COUNTER \ TIME_NOW”:无效
有人可以使用regexpression来说明如何获得有效/无效状态吗?
答案 0 :(得分:1)
\\0[1-9]COUNTER\\TIME_NOW
是您正在寻找的正则表达式。
#include <iostream>
#include <string>
#include <regex>
int main ()
{
if (std::regex_match ("\\02COUNTER\\TIME_NOW", std::regex("\\\\0[1-9]COUNTER\\\\TIME_NOW") ))
std::cout << "valid\n";
else
std::cout << "invalid\n";
if (std::regex_match ("\\10COUNTER\\TIME_NOW", std::regex("\\\\0[1-9]COUNTER\\\\TIME_NOW") ))
std::cout << "valid\n";
else
std::cout << "invalid\n";
if (std::regex_match ("\\00COUNTER\\TIME_NOW", std::regex("\\\\0[1-9]COUNTER\\\\TIME_NOW") ))
std::cout << "valid\n";
else
std::cout << "invalid\n";
if (std::regex_match ("\\88COUNTER\\TIME_NOW", std::regex("\\\\0[1-9]COUNTER\\\\TIME_NOW") ))
std::cout << "valid\n";
else
std::cout << "invalid\n";
return 0;
}
打印
valid
invalid
invalid
invalid