c# - C++ std::regex validate integer incorrectly -


this question has answer here:

i have simple regex express validate user input integer. in c# project, see validates correctly. below code in c#:

string string_to_validate = console.readline();     regex int_regex = new regex("[0-9]"); if (int_regex.ismatch(string_to_validate))     console.writeline("regex match. validation success!"); else     console.writeline("regex not match. validation fail!"); 

but in c++ see validates in correctly. validates correctly string lengh 1. below code in 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 match. validation success!"; else   std::cout << "regex not match. validation fail!"; 

please help. it's c++ problem or problem?

according msdn, c# method bool regex.ismatch(string)

indicates whether regular expression specified in regex constructor finds match in specified input string.

so return true if there @ least 1 digit in input string.


c++ std::regex_match

determines if regular expression matches entire target character sequence

so whole input string must contains digits pass regex.

to validate string integer of length in c++ have use regex:

 std::regex int_regex("[0-9]+"); // '+' - quantifier '1 or more' items range [0-9]  

or

 std::regex int_regex("\\d+"); 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -