我有一个简单的正则表达式来验证用户输入是整数。在C#项目中,我看到它正确验证。以下是我在C#中的代码:C + + std :: regex验证整数不正确
string string_to_validate = Console.ReadLine();
Regex int_regex = new Regex("[0-9]");
if (int_regex.IsMatch(string_to_validate))
Console.WriteLine("Regex is match. Validation is success!");
else
Console.WriteLine("Regex is not match. Validation is fail!");
但在C++中,我看到它在正确验证。它只验证正确的字符串,长度为1.下面是我在C++中的代码:
std::string string_to_validate;
std::cin >> string_to_validate;
std::regex int_regex("[0-9]");
if (std::regex_match(string_to_validate,
int_regex))
std::cout << "Regex is match. Validation is success!";
else
std::cout << "Regex is not match. Validation is fail!";
请帮忙。这是C++问题还是我的问题?
您展示的两个正则表达式是不一样的。 –
这两个正则表达式是不同的,'string_to_validate'的值是什么? –
你试图匹配什么? – revo