wex  v21.10.0
Public Types | Public Member Functions | List of all members
wex::regex Class Reference

This class offers regular expression matching. More...

#include <wex/regex.h>

Public Types

typedef std::vector< std::string > match_t
 Type that specifies the matches.
 
typedef std::function< void(const match_t &)> function_t
 Callback type.
 
typedef std::vector< std::string > regex_v_t
 Type that specifies a vector with regular expressions.
 
typedef std::vector< std::pair< std::string, function_t > > regex_v_c_t
 Type that specifies a vector with regular expressions and callbacks.
 

Public Member Functions

 regex (const std::string &regex, std::regex::flag_type flags=std::regex::ECMAScript)
 Constructor, provide regular expression string and regex flags.
 
 regex (const std::string &regex, function_t f, std::regex::flag_type flags=std::regex::ECMAScript)
 Constructor, provide regular expression string, callback and regex flags.
 
 regex (const regex_v_t &regex, std::regex::flag_type flags=std::regex::ECMAScript)
 Constructor, provide vector with regular expressions and regex flags.
 
 regex (const regex_v_c_t &regex, std::regex::flag_type flags=std::regex::ECMAScript)
 Constructor, provide vector with regular expressions, callbacks and regex flags.
 
int match (const std::string &text)
 Regular expression match. More...
 
const auto & matches () const
 Returns the (sub)matches.
 
const std::string & operator[] (size_t pos) const
 Returns reference to the requested submatch element.
 
bool replace (std::string &text, const std::string &replacement, std::regex_constants::match_flag_type=std::regex_constants::format_sed) const
 After match or search, replace text with replacement. More...
 
int search (const std::string &text)
 Regular expression search. More...
 
auto size () const
 Returns number of submatches.
 
const auto & which () const
 Returns the tuple element that matched.
 
auto which_no () const
 Returns the regex tuple no that matched, or -1 if no match was found.
 

Detailed Description

This class offers regular expression matching.

Member Function Documentation

◆ match()

int wex::regex::match ( const std::string &  text)

Regular expression match.

Returns:

  • -1: if text does not match or there is an error
  • 0: if text matches, but no submatches present.
  • >0: it fills submatches.

◆ replace()

bool wex::regex::replace ( std::string &  text,
const std::string &  replacement,
std::regex_constants::match_flag_type  = std::regex_constants::format_sed 
) const

After match or search, replace text with replacement.

Returns true if a regex is available, and regex_replace was invoked.

◆ search()

int wex::regex::search ( const std::string &  text)

Regular expression search.

Returns:

  • -1: if text does not match or there is an error
  • 0: if text matches, but no submatches present.
  • >0: it fills submatches.