Regex grouping matches with C++ 11 regex library -


i'm trying use regex group matching. want extract 2 strings 1 big string.

the input string looks this:

tХb:username!username@username.tcc.domain.com connected tХb:username!username@username.tcc.domain.com webmsg #username :this message tХb:username!username@username.tcc.domain.com status: visible 

the username can anything. same goes end part this message.

what want extract username comes after pound sign #. not other place in string, since can vary aswell. want message string comes after semicolon :.

i tried following regex. never outputs results.

regex rgx("webmsg #([a-za-z0-9]) :(.*?)"); smatch matches;  for(size_t i=0; i<matches.size(); ++i) {     cout << "match: " << matches[i] << endl; } 

i'm not getting matches. wrong regex?

your regular expression incorrect because neither capture group want. first looking match single character set [a-za-z0-9] followed <space>:, works single character usernames, nothing else. second capture group empty because you're looking 0 or more characters, specifying match should not greedy, means 0 character match valid result.

fixing both of these regex becomes

std::regex rgx("webmsg #([a-za-z0-9]+) :(.*)"); 

but instantiating regex , match_results object not produce matches, need apply regex algorithm. since want match part of input string appropriate algorithm use in case regex_search.

std::regex_search(s, matches, rgx); 

putting together

    std::string s{r"( tХb:username!username@username.tcc.domain.com connected tХb:username!username@username.tcc.domain.com webmsg #username :this message tХb:username!username@username.tcc.domain.com status: visible )"};      std::regex rgx("webmsg #([a-za-z0-9]+) :(.*)");     std::smatch matches;      if(std::regex_search(s, matches, rgx)) {         std::cout << "match found\n";          (size_t = 0; < matches.size(); ++i) {             std::cout << << ": '" << matches[i].str() << "'\n";         }     } else {         std::cout << "match not found\n";     } 

live demo


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -