userver: utils::match_results Class Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
utils::match_results Class Referencefinal

Your opinion will help to improve our service

Leave a feedback >

#include <userver/utils/regex.hpp>

Detailed Description

A drop-in replacement for std::match_results without huge includes and with better performance characteristics. Represents capturing groups of a single match result.

The group 0 always matches the whole pattern. User groups start with index 1.

Non-empty groups always point within the source string, so the position of a group within the source string can be obtained by subtracting .data() pointers or .begin() iterators.

Warning
The implementation can return empty groups as std::string_views with data() == nullptr or some invalid pointer with size() == 0. Check for emptiness before performing pointer arithmetic if a group can be empty according to the regex!
See also
utils::regex

Definition at line 97 of file regex.hpp.

Public Member Functions

 match_results ()
 
 match_results (const match_results &)
 
match_resultsoperator= (const match_results &)
 
std::size_t size () const
 
std::string_view operator[] (std::size_t sub) const
 
std::size_t position (std::size_t sub) const
 
std::size_t length (std::size_t sub) const
 
std::string_view prefix () const
 
std::string_view suffix () const
 

Constructor & Destructor Documentation

◆ match_results()

utils::match_results::match_results ( )

Constructs a null match_results, any usage except for copy/move is UB. Filled upon successful regex_match or regex_search.

Member Function Documentation

◆ length()

std::size_t utils::match_results::length ( std::size_t sub) const
Returns
the length of the capturing group at sub.
Note
Group 0 always matches the whole pattern. User groups start with index 1.

◆ operator[]()

std::string_view utils::match_results::operator[] ( std::size_t sub) const
Returns
the capturing group at sub.
Note
Group 0 always matches the whole pattern. User groups start with index 1.

◆ position()

std::size_t utils::match_results::position ( std::size_t sub) const
Returns
the position of the first character of the capturing group sub within the target (haystack) string.
Note
Group 0 always matches the whole pattern. User groups start with index 1.
Warning
For empty groups, calling this method is UB. Group 0 is always valid.

◆ prefix()

std::string_view utils::match_results::prefix ( ) const
Returns
the substring from the beginning of the target (haystack) string to the beginning of the full match.

◆ size()

std::size_t utils::match_results::size ( ) const
Returns
the number of capturing groups, including the group 0.

◆ suffix()

std::string_view utils::match_results::suffix ( ) const
Returns
the substring from the end of the full match to the end of the target (haystack) string.

Friends And Related Symbol Documentation

◆ regex_match

bool regex_match ( std::string_view str,
match_results & m,
const regex & pattern )
friend

Returns true if the specified regular expression matches the whole of the input. Fills in what matched in m.

Note
m may be clobbered on failure.

◆ regex_replace [1/2]

std::string regex_replace ( std::string_view str,
const regex & pattern,
Re2Replacement repl )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

See also
utils::Re2Replacement

◆ regex_replace [2/2]

std::string regex_replace ( std::string_view str,
const regex & pattern,
std::string_view repl )
friend

Create a new string where all regular expression matches replaced with repl.

Interprets repl as a literal, does not support substitutions.

See also
utils::Re2Replacement

◆ regex_search

bool regex_search ( std::string_view str,
match_results & m,
const regex & pattern )
friend

Determines whether the regular expression matches anywhere in the target character sequence. Fills in what matched in m.

Note
m may be clobbered on failure.

The documentation for this class was generated from the following file: