c++ find any string from a list in another string -
what options have find string list in string ?
with s being std::string, tried
s.find("cat" || "dog" || "cow" || "mouse", 0);
i want find first 1 of these strings , place in string ; if s "my cat sleeping\n" i'd 3 return value.
boost::to_upper(s);
was applied (for wondering).
you can regex.
i don't think there's way position of match directly, first have search regex, , if there match can search string. this:
#include <iostream> #include <string> #include <regex> using namespace std; int main() { string s = "my cat sleeping\n"; smatch m; regex animal("cat|dog|cow|mouse"); if (regex_search (s,m,animal)) { cout << "match found: " << m.str() << endl; size_t match_position = s.find(m.str()); // in case true, in general might want check if (match_position != string::npos) { cout << "first animal found at: " << match_position << endl; } } return 0; }
Comments
Post a Comment