C++ string of "1" does not match "1" (tried regex and boolean '==') -
see gif (visual studio debugger showing variables): http://www.elanhickler.com/transfer/regex_does_not_match.gif
bool stob(string s) { regex e("1"); bool b1 = (s == "1"); // false bool b2 = (string(s) == "1"); // false bool does_it_match1 = regex_match("1", e); // true bool does_it_match2 = regex_match(string(s), e); // false bool does_it_match3 = regex_match(string("1"), e); // true return does_it_match1; }
why not matching?
image shows input of s "1", more characters of 49("1")
, 0("\0")
ideone: https://ideone.com/b8luzf (this demosntrates problem, figured out answers below).
#include <iostream> #include <regex> #include <string> using namespace std; int main() { regex e("1"); string s = "1,"; s.back() = '\0'; cout << regex_match("1", e) << endl; cout << regex_match(s, e) << endl; return 0; }
unlike c-strings, start pointer points , end right before first 0-byte, std::string
s can contain 0-bytes. , seems case string. according debugger, string contains 2 characters, first of '1' , second '\0'.
so you're comparing 2-character string 1-character string , result therefore false.
Comments
Post a Comment