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?

http://i.stack.imgur.com/1llzk.png 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::strings 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

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -