Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/regex/match results"

From cppreference.com
< cpp‎ | regex
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (fmt)
 
(20 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{cpp/title|match_results}}
 
{{cpp/title|match_results}}
 
{{cpp/regex/match_results/navbar}}
 
{{cpp/regex/match_results/navbar}}
{{ddcl | header=regex | notes={{mark since c++11}} | 1=
+
{{
 +
header|
 +
{{sincec++11|1=
 
template<
 
template<
 
     class BidirIt,
 
     class BidirIt,
Line 7: Line 9:
 
> class match_results;
 
> class match_results;
 
}}
 
}}
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
The class template {{c|std::match_results}} holds a collection of character sequences that represent the result of a regular expression match.
+
The class template {{|std::match_results}} holds a collection of character sequences that represent the result of a regular expression match.
  
This is a specialized allocator-aware container. It can only be default created or modified by passing to the {{c|std::regex_search()}} or {{c|std::regex_match()}} algorithms.
+
This is a specialized allocator-aware container. It can only be default createdor modified by {{|std::regex_search}} or {{|std::regex_match}}.
  
The first sub_match (index 0) contained in a match_result always represents the full match within a target sequence made by a regex, and subsequent sub_matches represent sub-expression matches corresponding in sequence to the left parenthesis delimiting the sub-expression in the regex.
+
The first sub_match(index 0) contained in a match_resultalways represents the full match within a target sequence made by a regex, and subsequent represent sub-expression matches corresponding in sequence to the left parenthesis delimiting the sub-expression in the regex
 +
 
 +
.
  
 
===Type requirements===
 
===Type requirements===
{{param list begin}}
+
{{begin}}
{{param list req concept | BidirIt | BidirectionalIterator}}
+
{{req |BidirIt|BidirectionalIterator}}
{{param list req concept | Alloc | Allocator}}
+
{{req |Alloc|Allocator}}
{{param list end}}
+
{{end}}
  
 
===Specializations===
 
===Specializations===
 
Several specializations for common character sequence types are provided:  
 
Several specializations for common character sequence types are provided:  
  
{{dcl list begin}}
+
{{begin}}
{{dcl list header | regex}}
+
{{header|regex}}
{{dcl list hitem | Type | Definition}}
+
{{hitem|Type|Definition}}
{{dcl list item | {{tt|cmatch}} | {{c|match_results<const char*>}}}}
+
{{|{{tt|cmatch}}|{{c|match_results<const char*>}}}}
{{dcl list item | {{tt|wcmatch}} | {{c|match_results<const wchar_t*>}}}}
+
{{|{{tt|wcmatch}}|{{c|match_results<const wchar_t*>}}}}
{{dcl list item | {{tt|smatch}} | {{c|match_results<std::string::const_iterator>}}}}
+
{{|{{tt|smatch}}|{{c|match_results<std::string::const_iterator>}}}}
{{dcl list item | {{tt|wsmatch}} | {{c|match_results<std::wstring::const_iterator>}}}}
+
{{|{{tt|wsmatch}}|{{c|
{{dcl list end}}
+
 +
 +
 +
match_results<std::wstring::const_iterator>}}}}
 +
{{end}}
  
 
===Member types===
 
===Member types===
{{dcl list begin}}
+
{{begin}}
{{dcl list hitem | Member type | Definition}}
+
{{hitem|Member type|Definition}}
{{dcl list item | {{tt|allocator_type}} | {{c|Allocator}}}}
+
{{|{{tt|allocator_type}}|{{|Allocator}}}}
{{dcl list item | {{tt|value_type}} | {{c|std::sub_match<BidirIt>}}}}
+
{{|{{tt|value_type}}|{{c|std::sub_match<BidirIt>}}}}
{{dcl list item | {{tt|const_reference}} | {{c|const value_type&}}}}
+
{{|{{tt|const_reference}}|{{c|const value_type&}}}}
{{dcl list item | {{tt|reference}} | {{c|const_reference}}}}
+
{{|{{tt|reference}}|{{|}}}}
{{dcl list item | {{tt|const_iterator}} | ''implementation defined'' (depends on the underlying container)}}
+
{{|{{tt|const_iterator}}|''implementationdefined'' (depends on the underlying container)}}
{{dcl list item | {{tt|iterator}} | {{c|const_iterator}}}}
+
{{|{{tt|iterator}}|{{|const_iterator}}}}
{{dcl list item | {{tt|difference_type}} | {{c|std::iterator_traits<BidirIt>::difference_type}}}}
+
{{|{{tt|difference_type}}|{{c|std::iterator_traits<BidirIt>::difference_type}}}}
{{dcl list item | {{tt|size_type}} | {{c|std::allocator_traits<Alloc>::size_type}}}}
+
{{|{{tt|size_type}}|{{c|std::allocator_traits<Alloc>::size_type}}}}
{{dcl list item | {{tt|char_type}} | {{c|std::iterator_traits<BidirIt>::value_type}}}}
+
{{|{{tt|char_type}}|{{c|std::iterator_traits<BidirIt>::value_type}}}}
{{dcl list item | {{tt|string_type}} | {{c|std::basic_string<char_type>}}}}
+
{{|{{tt|string_type}}|{{c|std::basic_string<char_type>}}}}
{{dcl list end}}
+
{{end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/regex/match_results/dcl list constructor}}
+
{{|cpp/regex/match_results/constructor}}
{{dcl list template | cpp/regex/match_results/dcl list destructor}}
+
{{|cpp/regex/match_results/destructor}}
{{dcl list template | cpp/regex/match_results/dcl list operator{{=}}}}
+
{{|cpp/regex/match_results/operator{{=}}}}
{{dcl list template | cpp/regex/match_results/dcl list get_allocator}}
+
{{|cpp/regex/match_results/get_allocator}}
 
+
{{dcl list h2 | State}}
+
{{dcl list mem fun | cpp/regex/match_results/ready | }}
+
  
{{dcl list h2 | Size}}
+
{{h2|}}
{{dcl list template | cpp/regex/match_results/dcl list empty}}
+
{{|cpp/regex/match_results/|}}
{{dcl list template | cpp/regex/match_results/dcl list size}}
+
{{dcl list template | cpp/regex/match_results/dcl list max_size}}
+
  
{{dcl list h2 | Element access}}
+
{{h2|}}
{{dcl list template | cpp/regex/match_results/dcl list length}}
+
{{|cpp/regex/match_results/}}
{{dcl list template | cpp/regex/match_results/dcl list position}}
+
{{|cpp/regex/match_results/}}
{{dcl list template | cpp/regex/match_results/dcl list str}}
+
{{|cpp/regex/match_results/}}
{{dcl list template | cpp/regex/match_results/dcl list operator_at}}
+
{{dcl list template | cpp/regex/match_results/dcl list prefix}}
+
{{dcl list template | cpp/regex/match_results/dcl list suffix}}
+
  
{{dcl list h2 | Iterators}}
+
{{h2|}}
{{dcl list template | cpp/regex/match_results/dcl list begin}}
+
{{|cpp/regex/match_results/}}
{{dcl list template | cpp/regex/match_results/dcl list end}}
+
{{|cpp/regex/match_results/
 +
 +
 +
 +
}}
  
{{dcl list h2 | Format}}
+
{{h2|}}
{{dcl list template | cpp/regex/match_results/dcl list format}}
+
{{|cpp/regex/match_results/
 +
}}
  
{{dcl list h2 | Modifiers}}
+
{{h2|}}
{{dcl list template | cpp/regex/match_results/dcl list swap}}
+
{{|cpp/regex/match_results/}}
  
{{dcl list end}}
+
{{
 +
 +
end}}
  
 
===Non-member functions===
 
===Non-member functions===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/regex/match_results/dcl list operator_cmp}}
+
{{|cpp/regex/match_results/operator_cmp}}
{{dcl list template | cpp/regex/match_results/dcl list swap2}}
+
{{|cpp/regex/match_results/swap2}}
{{dcl list end}}
+
{{end}}
  
[[de:cpp/regex/match results]]
+
deesfritjaptruzh
[[es:cpp/regex/match results]]
+
[[fr:cpp/regex/match results]]
+
[[it:cpp/regex/match results]]
+
[[ja:cpp/regex/match results]]
+
[[pt:cpp/regex/match results]]
+
[[ru:cpp/regex/match results]]
+
[[zh:cpp/regex/match results]]
+

Latest revision as of 07:15, 25 December 2023

 
 
 
Regular expressions library
Classes
(C++11)
match_results
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
 
 
Defined in header <regex>
template<

    class BidirIt,
    class Alloc = std::allocator<std::sub_match<BidirIt>>

> class match_results;
(1) (since C++11)
namespace pmr {

    template <class BidirIt>
    using match_results = std::match_results<BidirIt,
                              std::pmr::polymorphic_allocator<
                                  std::sub_match<BidirIt>>>;

}
(2) (since C++17)

The class template std::match_results holds a collection of character sequences that represent the result of a regular expression match.

This is a specialized allocator-aware container. It can only be default created, obtained from std::regex_iterator, or modified by std::regex_search or std::regex_match. Because std::match_results holds std::sub_matches, each of which is a pair of iterators into the original character sequence that was matched, it's undefined behavior to examine std::match_results if the original character sequence was destroyed or iterators to it were invalidated for other reasons.

The first std::sub_match (index 0) contained in a std::match_result always represents the full match within a target sequence made by a regex, and subsequent std::sub_matches represent sub-expression matches corresponding in sequence to the left parenthesis delimiting the sub-expression in the regex.

std::match_results meets the requirements of a AllocatorAwareContainer and of a SequenceContainer, except that only copy assignment, move assignment, and operations defined for a constant containers are supported, and that the semantics of comparison functions are different from those required for a container.

Contents

[edit] Type requirements

-
BidirIt must meet the requirements of LegacyBidirectionalIterator.
-
Alloc must meet the requirements of Allocator.

[edit] Specializations

Several specializations for common character sequence types are provided:

Defined in header <regex>
Type Definition
std::cmatch std::match_results<const char*>
std::wcmatch std::match_results<const wchar_t*>
std::smatch std::match_results<std::string::const_iterator>
std::wsmatch std::match_results<std::wstring::const_iterator>
std::pmr::cmatch (C++17) std::pmr::match_results<const char*>
std::pmr::wcmatch (C++17) std::pmr::match_results<const wchar_t*>
std::pmr::smatch (C++17) std::pmr::match_results<std::string::const_iterator>
std::pmr::wsmatch (C++17) std::pmr::match_results<std::wstring::const_iterator>

[edit] Member types

Member type Definition
allocator_type Allocator
value_type std::sub_match<BidirIt>
const_reference const value_type&
reference value_type&
const_iterator implementation-defined (depends on the underlying container)
iterator const_iterator
difference_type std::iterator_traits<BidirIt>::difference_type
size_type std::allocator_traits<Alloc>::size_type
char_type std::iterator_traits<BidirIt>::value_type
string_type std::basic_string<char_type>

[edit] Member functions

constructs the object
(public member function) [edit]
destructs the object
(public member function) [edit]
assigns the contents
(public member function) [edit]
returns the associated allocator
(public member function) [edit]
State
checks if the results are available
(public member function)
Size
checks whether the match was successful
(public member function) [edit]
returns the number of matches in a fully-established result state
(public member function) [edit]
returns the maximum possible number of sub-matches
(public member function) [edit]
Element access
returns the length of the particular sub-match
(public member function) [edit]
returns the position of the first character of the particular sub-match
(public member function) [edit]
returns the sequence of characters for the particular sub-match
(public member function) [edit]
returns specified sub-match
(public member function) [edit]
returns sub-sequence between the beginning of the target sequence and the beginning of the full match
(public member function) [edit]
returns sub-sequence between the end of the full match and the end of the target sequence
(public member function) [edit]
Iterators
returns iterator to the beginning of the list of sub-matches
(public member function) [edit]
returns iterator to the end of the list of sub-matches
(public member function) [edit]
Format
formats match results for output
(public member function) [edit]
Modifiers
swaps the contents
(public member function) [edit]

[edit] Non-member functions

(removed in C++20)
lexicographically compares the values in the two match result
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]