Libreria C ++ Regex - regex_search

Descrizione

Restituisce se qualche sotto-sequenza nella sequenza di destinazione (il soggetto) corrisponde all'espressione regolare rgx (il modello). La sequenza di destinazione è s o la sequenza di caratteri tra il primo e l'ultimo, a seconda della versione utilizzata.

Dichiarazione

Di seguito è riportata la dichiarazione per std :: regex_search.

template <class charT, class traits>
   bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
   regex_constants::match_flag_type flags = regex_constants::match_default);

C ++ 11

template <class charT, class traits>
   bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
   regex_constants::match_flag_type flags = regex_constants::match_default);

C ++ 14

template <class charT, class traits>
  bool regex_search (const charT* s, const basic_regex<charT,traits>& rgx,
          regex_constants::match_flag_type flags = regex_constants::match_default);

Parametri

  • s - È una stringa con la sequenza di destinazione.

  • rgx - È un oggetto basic_regex da abbinare.

  • flags - Viene utilizzato per controllare come rgx è abbinato.

  • m - È un oggetto di tipo match_results.

Valore di ritorno

Restituisce vero se rgx corrisponde a una sotto-sequenza nella sequenza di destinazione. altrimenti falso.

Eccezioni

No-noexcept - questa funzione membro non genera mai eccezioni.

Esempio

Nell'esempio seguente per std :: regex_search.

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

int main () {
   std::string s ("this subject has a submarine as a subsequence");
   std::smatch m;
   std::regex e ("\\b(sub)([^ ]*)");

   std::cout << "Target sequence: " << s << std::endl;
   std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
   std::cout << "The following matches and submatches were found:" << std::endl;

   while (std::regex_search (s,m,e)) {
      for (auto x:m) std::cout << x << " ";
      std::cout << std::endl;
      s = m.suffix().str();
   }

   return 0;
}

L'output dovrebbe essere così -

Target sequence: this subject has a submarine as a subsequence
Regular expression: /\b(sub)([^ ]*)/
The following matches and submatches were found:
subject sub ject 
submarine sub marine 
subsequence sub sequence