mercredi 7 septembre 2022

How to find the smiley symbols( :) and :-] ) starting positions in a text using c++

Trying to find the starting positions of the smiley in c++. But once it found the first smiley, it stops finding the next smiley.

Code I was trying with regex (":\)|:\-\]")

#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("Best :) bookseller :) today. :-]");
  std::smatch m;
  std::regex e (":\\)|:\\-\\]");

  if (std::regex_search(s, m, e))
  {
     std::cout << "matched" << std::endl;
     std::cout << m[0] << " -> " << m.position(0) << std::endl;
     std::cout << m[1] << " -> " << m.position(1) << std::endl;
     std::cout << m[2] << " -> " << m.position(2) << std::endl;
  }
  else
     std::cout << "not matched" << std::endl;

  return 0;
}

Expected output is:

matched
:) -> 5
:) -> 19
:-] -> 29

But getting output as:

matched
:) -> 5
-> 32
-> 32

It would be great if someone suggest how to find the smiley symbols position in multiple places in a test using c++ (or) suggest a right regex to use.

Aucun commentaire:

Enregistrer un commentaire