1 // class template regex -*- C++ -*-
3 // Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{regex}
31 namespace std _GLIBCXX_VISIBILITY(default)
33 _GLIBCXX_BEGIN_NAMESPACE_VERSION
34 _GLIBCXX_BEGIN_NAMESPACE_CXX11
35 template<typename, typename>
38 template<typename, typename>
41 _GLIBCXX_END_NAMESPACE_CXX11
45 enum class _RegexExecutorPolicy : int
46 { _S_auto, _S_alternate };
48 template<typename _BiIter, typename _Alloc,
49 typename _CharT, typename _TraitsT,
50 _RegexExecutorPolicy __policy,
53 __regex_algo_impl(_BiIter __s,
55 match_results<_BiIter, _Alloc>& __m,
56 const basic_regex<_CharT, _TraitsT>& __re,
57 regex_constants::match_flag_type __flags);
59 template<typename, typename, typename, bool>
63 _GLIBCXX_BEGIN_NAMESPACE_CXX11
71 * @brief Describes aspects of a regular expression.
73 * A regular expression traits class that satisfies the requirements of
76 * The class %regex is parameterized around a set of related types and
77 * functions used to complete the definition of its semantics. This class
78 * satisfies the requirements of such a traits class.
80 template<typename _Ch_type>
84 typedef _Ch_type char_type;
85 typedef std::basic_string<char_type> string_type;
86 typedef std::locale locale_type;
90 typedef std::ctype_base::mask _BaseType;
92 unsigned char _M_extended;
93 static constexpr unsigned char _S_under = 1 << 0;
94 static constexpr unsigned char _S_valid_mask = 0x1;
96 constexpr _RegexMask(_BaseType __base = 0,
97 unsigned char __extended = 0)
98 : _M_base(__base), _M_extended(__extended)
102 operator&(_RegexMask __other) const
104 return _RegexMask(_M_base & __other._M_base,
105 _M_extended & __other._M_extended);
109 operator|(_RegexMask __other) const
111 return _RegexMask(_M_base | __other._M_base,
112 _M_extended | __other._M_extended);
116 operator^(_RegexMask __other) const
118 return _RegexMask(_M_base ^ __other._M_base,
119 _M_extended ^ __other._M_extended);
124 { return _RegexMask(~_M_base, ~_M_extended); }
127 operator&=(_RegexMask __other)
128 { return *this = (*this) & __other; }
131 operator|=(_RegexMask __other)
132 { return *this = (*this) | __other; }
135 operator^=(_RegexMask __other)
136 { return *this = (*this) ^ __other; }
139 operator==(_RegexMask __other) const
141 return (_M_extended & _S_valid_mask)
142 == (__other._M_extended & _S_valid_mask)
143 && _M_base == __other._M_base;
147 operator!=(_RegexMask __other) const
148 { return !((*this) == __other); }
152 typedef _RegexMask char_class_type;
156 * @brief Constructs a default traits object.
161 * @brief Gives the length of a C-style string starting at @p __p.
163 * @param __p a pointer to the start of a character sequence.
165 * @returns the number of characters between @p *__p and the first
166 * default-initialized value of type @p char_type. In other words, uses
167 * the C-string algorithm for determining the length of a sequence of
171 length(const char_type* __p)
172 { return string_type::traits_type::length(__p); }
175 * @brief Performs the identity translation.
177 * @param __c A character to the locale-specific character set.
182 translate(char_type __c) const
186 * @brief Translates a character into a case-insensitive equivalent.
188 * @param __c A character to the locale-specific character set.
190 * @returns the locale-specific lower-case equivalent of __c.
191 * @throws std::bad_cast if the imbued locale does not support the ctype
195 translate_nocase(char_type __c) const
197 typedef std::ctype<char_type> __ctype_type;
198 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
199 return __fctyp.tolower(__c);
203 * @brief Gets a sort key for a character sequence.
205 * @param __first beginning of the character sequence.
206 * @param __last one-past-the-end of the character sequence.
208 * Returns a sort key for the character sequence designated by the
209 * iterator range [F1, F2) such that if the character sequence [G1, G2)
210 * sorts before the character sequence [H1, H2) then
211 * v.transform(G1, G2) < v.transform(H1, H2).
213 * What this really does is provide a more efficient way to compare a
214 * string to multiple other strings in locales with fancy collation
215 * rules and equivalence classes.
217 * @returns a locale-specific sort key equivalent to the input range.
219 * @throws std::bad_cast if the current locale does not have a collate
222 template<typename _Fwd_iter>
224 transform(_Fwd_iter __first, _Fwd_iter __last) const
226 typedef std::collate<char_type> __collate_type;
227 const __collate_type& __fclt(use_facet<__collate_type>(_M_locale));
228 string_type __s(__first, __last);
229 return __fclt.transform(__s.data(), __s.data() + __s.size());
233 * @brief Gets a sort key for a character sequence, independent of case.
235 * @param __first beginning of the character sequence.
236 * @param __last one-past-the-end of the character sequence.
238 * Effects: if typeid(use_facet<collate<_Ch_type> >) ==
239 * typeid(collate_byname<_Ch_type>) and the form of the sort key
240 * returned by collate_byname<_Ch_type>::transform(__first, __last)
241 * is known and can be converted into a primary sort key
242 * then returns that key, otherwise returns an empty string.
244 * @todo Implement this function correctly.
246 template<typename _Fwd_iter>
248 transform_primary(_Fwd_iter __first, _Fwd_iter __last) const
250 // TODO : this is not entirely correct.
251 // This function requires extra support from the platform.
253 // Read http://gcc.gnu.org/ml/libstdc++/2013-09/msg00117.html and
254 // http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2003/n1429.htm
256 typedef std::ctype<char_type> __ctype_type;
257 const __ctype_type& __fctyp(use_facet<__ctype_type>(_M_locale));
258 std::vector<char_type> __s(__first, __last);
259 __fctyp.tolower(__s.data(), __s.data() + __s.size());
260 return this->transform(__s.data(), __s.data() + __s.size());
264 * @brief Gets a collation element by name.
266 * @param __first beginning of the collation element name.
267 * @param __last one-past-the-end of the collation element name.
269 * @returns a sequence of one or more characters that represents the
270 * collating element consisting of the character sequence designated by
271 * the iterator range [__first, __last). Returns an empty string if the
272 * character sequence is not a valid collating element.
274 template<typename _Fwd_iter>
276 lookup_collatename(_Fwd_iter __first, _Fwd_iter __last) const;
279 * @brief Maps one or more characters to a named character
282 * @param __first beginning of the character sequence.
283 * @param __last one-past-the-end of the character sequence.
284 * @param __icase ignores the case of the classification name.
286 * @returns an unspecified value that represents the character
287 * classification named by the character sequence designated by
288 * the iterator range [__first, __last). If @p icase is true,
289 * the returned mask identifies the classification regardless of
290 * the case of the characters to be matched (for example,
291 * [[:lower:]] is the same as [[:alpha:]]), otherwise a
292 * case-dependent classification is returned. The value
293 * returned shall be independent of the case of the characters
294 * in the character sequence. If the name is not recognized then
295 * returns a value that compares equal to 0.
297 * At least the following names (or their wide-character equivalent) are
315 template<typename _Fwd_iter>
317 lookup_classname(_Fwd_iter __first, _Fwd_iter __last,
318 bool __icase = false) const;
321 * @brief Determines if @p c is a member of an identified class.
323 * @param __c a character.
324 * @param __f a class type (as returned from lookup_classname).
326 * @returns true if the character @p __c is a member of the classification
327 * represented by @p __f, false otherwise.
329 * @throws std::bad_cast if the current locale does not have a ctype
333 isctype(_Ch_type __c, char_class_type __f) const;
336 * @brief Converts a digit to an int.
338 * @param __ch a character representing a digit.
339 * @param __radix the radix if the numeric conversion (limited to 8, 10,
342 * @returns the value represented by the digit __ch in base radix if the
343 * character __ch is a valid digit in base radix; otherwise returns -1.
346 value(_Ch_type __ch, int __radix) const;
349 * @brief Imbues the regex_traits object with a copy of a new locale.
351 * @param __loc A locale.
353 * @returns a copy of the previous locale in use by the regex_traits
356 * @note Calling imbue with a different locale than the one currently in
357 * use invalidates all cached data held by *this.
360 imbue(locale_type __loc)
362 std::swap(_M_locale, __loc);
367 * @brief Gets a copy of the current locale in use by the regex_traits
372 { return _M_locale; }
375 locale_type _M_locale;
378 // [7.8] Class basic_regex
380 * Objects of specializations of this class represent regular expressions
381 * constructed from sequences of character type @p _Ch_type.
383 * Storage for the regular expression is allocated and deallocated as
384 * necessary by the member functions of this class.
386 template<typename _Ch_type, typename _Rx_traits = regex_traits<_Ch_type>>
390 static_assert(is_same<_Ch_type, typename _Rx_traits::char_type>::value,
391 "regex traits class must have the same char_type");
394 typedef _Ch_type value_type;
395 typedef _Rx_traits traits_type;
396 typedef typename traits_type::string_type string_type;
397 typedef regex_constants::syntax_option_type flag_type;
398 typedef typename traits_type::locale_type locale_type;
405 static constexpr flag_type icase = regex_constants::icase;
406 static constexpr flag_type nosubs = regex_constants::nosubs;
407 static constexpr flag_type optimize = regex_constants::optimize;
408 static constexpr flag_type collate = regex_constants::collate;
409 static constexpr flag_type ECMAScript = regex_constants::ECMAScript;
410 static constexpr flag_type basic = regex_constants::basic;
411 static constexpr flag_type extended = regex_constants::extended;
412 static constexpr flag_type awk = regex_constants::awk;
413 static constexpr flag_type grep = regex_constants::grep;
414 static constexpr flag_type egrep = regex_constants::egrep;
417 // [7.8.2] construct/copy/destroy
419 * Constructs a basic regular expression that does not match any
420 * character sequence.
423 : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr)
427 * @brief Constructs a basic regular expression from the
428 * sequence [__p, __p + char_traits<_Ch_type>::length(__p))
429 * interpreted according to the flags in @p __f.
431 * @param __p A pointer to the start of a C-style null-terminated string
432 * containing a regular expression.
433 * @param __f Flags indicating the syntax rules and options.
435 * @throws regex_error if @p __p is not a valid regular expression.
438 basic_regex(const _Ch_type* __p, flag_type __f = ECMAScript)
439 : basic_regex(__p, __p + char_traits<_Ch_type>::length(__p), __f)
443 * @brief Constructs a basic regular expression from the sequence
444 * [p, p + len) interpreted according to the flags in @p f.
446 * @param __p A pointer to the start of a string containing a regular
448 * @param __len The length of the string containing the regular
450 * @param __f Flags indicating the syntax rules and options.
452 * @throws regex_error if @p __p is not a valid regular expression.
454 basic_regex(const _Ch_type* __p, std::size_t __len,
455 flag_type __f = ECMAScript)
456 : basic_regex(__p, __p + __len, __f)
460 * @brief Copy-constructs a basic regular expression.
462 * @param __rhs A @p regex object.
464 basic_regex(const basic_regex& __rhs) = default;
467 * @brief Move-constructs a basic regular expression.
469 * @param __rhs A @p regex object.
471 basic_regex(basic_regex&& __rhs) noexcept = default;
474 * @brief Constructs a basic regular expression from the string
475 * @p s interpreted according to the flags in @p f.
477 * @param __s A string containing a regular expression.
478 * @param __f Flags indicating the syntax rules and options.
480 * @throws regex_error if @p __s is not a valid regular expression.
482 template<typename _Ch_traits, typename _Ch_alloc>
484 basic_regex(const std::basic_string<_Ch_type, _Ch_traits,
486 flag_type __f = ECMAScript)
487 : basic_regex(__s.data(), __s.data() + __s.size(), __f)
491 * @brief Constructs a basic regular expression from the range
492 * [first, last) interpreted according to the flags in @p f.
494 * @param __first The start of a range containing a valid regular
496 * @param __last The end of a range containing a valid regular
498 * @param __f The format flags of the regular expression.
500 * @throws regex_error if @p [__first, __last) is not a valid regular
503 template<typename _FwdIter>
504 basic_regex(_FwdIter __first, _FwdIter __last,
505 flag_type __f = ECMAScript)
506 : basic_regex(std::move(__first), std::move(__last), locale_type(), __f)
510 * @brief Constructs a basic regular expression from an initializer list.
512 * @param __l The initializer list.
513 * @param __f The format flags of the regular expression.
515 * @throws regex_error if @p __l is not a valid regular expression.
517 basic_regex(initializer_list<_Ch_type> __l, flag_type __f = ECMAScript)
518 : basic_regex(__l.begin(), __l.end(), __f)
522 * @brief Destroys a basic regular expression.
528 * @brief Assigns one regular expression to another.
531 operator=(const basic_regex& __rhs)
532 { return this->assign(__rhs); }
535 * @brief Move-assigns one regular expression to another.
538 operator=(basic_regex&& __rhs) noexcept
539 { return this->assign(std::move(__rhs)); }
542 * @brief Replaces a regular expression with a new one constructed from
543 * a C-style null-terminated string.
545 * @param __p A pointer to the start of a null-terminated C-style string
546 * containing a regular expression.
549 operator=(const _Ch_type* __p)
550 { return this->assign(__p); }
553 * @brief Replaces a regular expression with a new one constructed from
554 * an initializer list.
556 * @param __l The initializer list.
558 * @throws regex_error if @p __l is not a valid regular expression.
561 operator=(initializer_list<_Ch_type> __l)
562 { return this->assign(__l.begin(), __l.end()); }
565 * @brief Replaces a regular expression with a new one constructed from
568 * @param __s A pointer to a string containing a regular expression.
570 template<typename _Ch_traits, typename _Alloc>
572 operator=(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s)
573 { return this->assign(__s); }
577 * @brief the real assignment operator.
579 * @param __rhs Another regular expression object.
582 assign(const basic_regex& __rhs)
584 basic_regex __tmp(__rhs);
590 * @brief The move-assignment operator.
592 * @param __rhs Another regular expression object.
595 assign(basic_regex&& __rhs) noexcept
597 basic_regex __tmp(std::move(__rhs));
603 * @brief Assigns a new regular expression to a regex object from a
604 * C-style null-terminated string containing a regular expression
607 * @param __p A pointer to a C-style null-terminated string containing
608 * a regular expression pattern.
609 * @param __flags Syntax option flags.
611 * @throws regex_error if __p does not contain a valid regular
612 * expression pattern interpreted according to @p __flags. If
613 * regex_error is thrown, *this remains unchanged.
616 assign(const _Ch_type* __p, flag_type __flags = ECMAScript)
617 { return this->assign(string_type(__p), __flags); }
620 * @brief Assigns a new regular expression to a regex object from a
621 * C-style string containing a regular expression pattern.
623 * @param __p A pointer to a C-style string containing a
624 * regular expression pattern.
625 * @param __len The length of the regular expression pattern string.
626 * @param __flags Syntax option flags.
628 * @throws regex_error if p does not contain a valid regular
629 * expression pattern interpreted according to @p __flags. If
630 * regex_error is thrown, *this remains unchanged.
633 assign(const _Ch_type* __p, std::size_t __len, flag_type __flags)
634 { return this->assign(string_type(__p, __len), __flags); }
637 * @brief Assigns a new regular expression to a regex object from a
638 * string containing a regular expression pattern.
640 * @param __s A string containing a regular expression pattern.
641 * @param __flags Syntax option flags.
643 * @throws regex_error if __s does not contain a valid regular
644 * expression pattern interpreted according to @p __flags. If
645 * regex_error is thrown, *this remains unchanged.
647 template<typename _Ch_traits, typename _Alloc>
649 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s,
650 flag_type __flags = ECMAScript)
652 return this->assign(basic_regex(__s.data(), __s.data() + __s.size(),
657 * @brief Assigns a new regular expression to a regex object.
659 * @param __first The start of a range containing a valid regular
661 * @param __last The end of a range containing a valid regular
663 * @param __flags Syntax option flags.
665 * @throws regex_error if p does not contain a valid regular
666 * expression pattern interpreted according to @p __flags. If
667 * regex_error is thrown, the object remains unchanged.
669 template<typename _InputIterator>
671 assign(_InputIterator __first, _InputIterator __last,
672 flag_type __flags = ECMAScript)
673 { return this->assign(string_type(__first, __last), __flags); }
676 * @brief Assigns a new regular expression to a regex object.
678 * @param __l An initializer list representing a regular expression.
679 * @param __flags Syntax option flags.
681 * @throws regex_error if @p __l does not contain a valid
682 * regular expression pattern interpreted according to @p
683 * __flags. If regex_error is thrown, the object remains
687 assign(initializer_list<_Ch_type> __l, flag_type __flags = ECMAScript)
688 { return this->assign(__l.begin(), __l.end(), __flags); }
690 // [7.8.4] const operations
692 * @brief Gets the number of marked subexpressions within the regular
699 return _M_automaton->_M_sub_count() - 1;
704 * @brief Gets the flags used to construct the regular expression
705 * or in the last call to assign().
713 * @brief Imbues the regular expression object with the given locale.
715 * @param __loc A locale.
718 imbue(locale_type __loc)
720 std::swap(__loc, _M_loc);
721 _M_automaton.reset();
726 * @brief Gets the locale currently imbued in the regular expression
735 * @brief Swaps the contents of two regular expression objects.
737 * @param __rhs Another regular expression object.
740 swap(basic_regex& __rhs)
742 std::swap(_M_flags, __rhs._M_flags);
743 std::swap(_M_loc, __rhs._M_loc);
744 std::swap(_M_automaton, __rhs._M_automaton);
747 #ifdef _GLIBCXX_DEBUG
749 _M_dot(std::ostream& __ostr)
750 { _M_automaton->_M_dot(__ostr); }
754 typedef std::shared_ptr<const __detail::_NFA<_Rx_traits>> _AutomatonPtr;
756 template<typename _FwdIter>
757 basic_regex(_FwdIter __first, _FwdIter __last, locale_type __loc,
759 : _M_flags(__f), _M_loc(std::move(__loc)),
760 _M_automaton(__detail::__compile_nfa<_Rx_traits>(
761 std::move(__first), std::move(__last), _M_loc, _M_flags))
764 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
765 __detail::_RegexExecutorPolicy, bool>
767 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
768 const basic_regex<_Cp, _Rp>&,
769 regex_constants::match_flag_type);
771 template<typename, typename, typename, bool>
772 friend class __detail::_Executor;
776 _AutomatonPtr _M_automaton;
779 #if __cplusplus < 201703L
780 template<typename _Ch, typename _Tr>
781 constexpr regex_constants::syntax_option_type
782 basic_regex<_Ch, _Tr>::icase;
784 template<typename _Ch, typename _Tr>
785 constexpr regex_constants::syntax_option_type
786 basic_regex<_Ch, _Tr>::nosubs;
788 template<typename _Ch, typename _Tr>
789 constexpr regex_constants::syntax_option_type
790 basic_regex<_Ch, _Tr>::optimize;
792 template<typename _Ch, typename _Tr>
793 constexpr regex_constants::syntax_option_type
794 basic_regex<_Ch, _Tr>::collate;
796 template<typename _Ch, typename _Tr>
797 constexpr regex_constants::syntax_option_type
798 basic_regex<_Ch, _Tr>::ECMAScript;
800 template<typename _Ch, typename _Tr>
801 constexpr regex_constants::syntax_option_type
802 basic_regex<_Ch, _Tr>::basic;
804 template<typename _Ch, typename _Tr>
805 constexpr regex_constants::syntax_option_type
806 basic_regex<_Ch, _Tr>::extended;
808 template<typename _Ch, typename _Tr>
809 constexpr regex_constants::syntax_option_type
810 basic_regex<_Ch, _Tr>::awk;
812 template<typename _Ch, typename _Tr>
813 constexpr regex_constants::syntax_option_type
814 basic_regex<_Ch, _Tr>::grep;
816 template<typename _Ch, typename _Tr>
817 constexpr regex_constants::syntax_option_type
818 basic_regex<_Ch, _Tr>::egrep;
821 #if __cpp_deduction_guides >= 201606
822 template<typename _ForwardIterator>
823 basic_regex(_ForwardIterator, _ForwardIterator,
824 regex_constants::syntax_option_type = {})
825 -> basic_regex<typename iterator_traits<_ForwardIterator>::value_type>;
828 /** @brief Standard regular expressions. */
829 typedef basic_regex<char> regex;
831 #ifdef _GLIBCXX_USE_WCHAR_T
832 /** @brief Standard wide-character regular expressions. */
833 typedef basic_regex<wchar_t> wregex;
837 // [7.8.6] basic_regex swap
839 * @brief Swaps the contents of two regular expression objects.
840 * @param __lhs First regular expression.
841 * @param __rhs Second regular expression.
843 template<typename _Ch_type, typename _Rx_traits>
845 swap(basic_regex<_Ch_type, _Rx_traits>& __lhs,
846 basic_regex<_Ch_type, _Rx_traits>& __rhs)
847 { __lhs.swap(__rhs); }
850 // [7.9] Class template sub_match
852 * A sequence of characters matched by a particular marked sub-expression.
854 * An object of this class is essentially a pair of iterators marking a
855 * matched subexpression within a regular expression pattern match. Such
856 * objects can be converted to and compared with std::basic_string objects
857 * of a similar base character type as the pattern matched by the regular
860 * The iterators that make up the pair are the usual half-open interval
861 * referencing the actual original pattern matched.
863 template<typename _BiIter>
864 class sub_match : public std::pair<_BiIter, _BiIter>
866 typedef iterator_traits<_BiIter> __iter_traits;
869 typedef typename __iter_traits::value_type value_type;
870 typedef typename __iter_traits::difference_type difference_type;
871 typedef _BiIter iterator;
872 typedef std::basic_string<value_type> string_type;
876 constexpr sub_match() : matched() { }
879 * Gets the length of the matching sequence.
883 { return this->matched ? std::distance(this->first, this->second) : 0; }
886 * @brief Gets the matching sequence as a string.
888 * @returns the matching sequence as a string.
890 * This is the implicit conversion operator. It is identical to the
891 * str() member function except that it will want to pop up in
892 * unexpected places and cause a great deal of confusion and cursing
895 operator string_type() const
898 ? string_type(this->first, this->second)
903 * @brief Gets the matching sequence as a string.
905 * @returns the matching sequence as a string.
911 ? string_type(this->first, this->second)
916 * @brief Compares this and another matched sequence.
918 * @param __s Another matched sequence to compare to this one.
920 * @retval <0 this matched sequence will collate before @p __s.
921 * @retval =0 this matched sequence is equivalent to @p __s.
922 * @retval <0 this matched sequence will collate after @p __s.
925 compare(const sub_match& __s) const
926 { return this->str().compare(__s.str()); }
929 * @brief Compares this sub_match to a string.
931 * @param __s A string to compare to this sub_match.
933 * @retval <0 this matched sequence will collate before @p __s.
934 * @retval =0 this matched sequence is equivalent to @p __s.
935 * @retval <0 this matched sequence will collate after @p __s.
938 compare(const string_type& __s) const
939 { return this->str().compare(__s); }
942 * @brief Compares this sub_match to a C-style string.
944 * @param __s A C-style string to compare to this sub_match.
946 * @retval <0 this matched sequence will collate before @p __s.
947 * @retval =0 this matched sequence is equivalent to @p __s.
948 * @retval <0 this matched sequence will collate after @p __s.
951 compare(const value_type* __s) const
952 { return this->str().compare(__s); }
956 /** @brief Standard regex submatch over a C-style null-terminated string. */
957 typedef sub_match<const char*> csub_match;
959 /** @brief Standard regex submatch over a standard string. */
960 typedef sub_match<string::const_iterator> ssub_match;
962 #ifdef _GLIBCXX_USE_WCHAR_T
963 /** @brief Regex submatch over a C-style null-terminated wide string. */
964 typedef sub_match<const wchar_t*> wcsub_match;
966 /** @brief Regex submatch over a standard wide string. */
967 typedef sub_match<wstring::const_iterator> wssub_match;
970 // [7.9.2] sub_match non-member operators
973 * @brief Tests the equivalence of two regular expression submatches.
974 * @param __lhs First regular expression submatch.
975 * @param __rhs Second regular expression submatch.
976 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
978 template<typename _BiIter>
980 operator==(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
981 { return __lhs.compare(__rhs) == 0; }
984 * @brief Tests the inequivalence of two regular expression submatches.
985 * @param __lhs First regular expression submatch.
986 * @param __rhs Second regular expression submatch.
987 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
989 template<typename _BiIter>
991 operator!=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
992 { return __lhs.compare(__rhs) != 0; }
995 * @brief Tests the ordering of two regular expression submatches.
996 * @param __lhs First regular expression submatch.
997 * @param __rhs Second regular expression submatch.
998 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1000 template<typename _BiIter>
1002 operator<(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1003 { return __lhs.compare(__rhs) < 0; }
1006 * @brief Tests the ordering of two regular expression submatches.
1007 * @param __lhs First regular expression submatch.
1008 * @param __rhs Second regular expression submatch.
1009 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1011 template<typename _BiIter>
1013 operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1014 { return __lhs.compare(__rhs) <= 0; }
1017 * @brief Tests the ordering of two regular expression submatches.
1018 * @param __lhs First regular expression submatch.
1019 * @param __rhs Second regular expression submatch.
1020 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1022 template<typename _BiIter>
1024 operator>=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1025 { return __lhs.compare(__rhs) >= 0; }
1028 * @brief Tests the ordering of two regular expression submatches.
1029 * @param __lhs First regular expression submatch.
1030 * @param __rhs Second regular expression submatch.
1031 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1033 template<typename _BiIter>
1035 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
1036 { return __lhs.compare(__rhs) > 0; }
1038 // Alias for sub_match'd string.
1039 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1040 using __sub_match_string = basic_string<
1041 typename iterator_traits<_Bi_iter>::value_type,
1042 _Ch_traits, _Ch_alloc>;
1045 * @brief Tests the equivalence of a string and a regular expression
1047 * @param __lhs A string.
1048 * @param __rhs A regular expression submatch.
1049 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1051 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1053 operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1054 const sub_match<_Bi_iter>& __rhs)
1056 typedef typename sub_match<_Bi_iter>::string_type string_type;
1057 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) == 0;
1061 * @brief Tests the inequivalence of a string and a regular expression
1063 * @param __lhs A string.
1064 * @param __rhs A regular expression submatch.
1065 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1067 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1069 operator!=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1070 const sub_match<_Bi_iter>& __rhs)
1071 { return !(__lhs == __rhs); }
1074 * @brief Tests the ordering of a string and a regular expression submatch.
1075 * @param __lhs A string.
1076 * @param __rhs A regular expression submatch.
1077 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1079 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1081 operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1082 const sub_match<_Bi_iter>& __rhs)
1084 typedef typename sub_match<_Bi_iter>::string_type string_type;
1085 return __rhs.compare(string_type(__lhs.data(), __lhs.size())) > 0;
1089 * @brief Tests the ordering of a string and a regular expression submatch.
1090 * @param __lhs A string.
1091 * @param __rhs A regular expression submatch.
1092 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1094 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1096 operator>(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1097 const sub_match<_Bi_iter>& __rhs)
1098 { return __rhs < __lhs; }
1101 * @brief Tests the ordering of a string and a regular expression submatch.
1102 * @param __lhs A string.
1103 * @param __rhs A regular expression submatch.
1104 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1106 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1108 operator>=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1109 const sub_match<_Bi_iter>& __rhs)
1110 { return !(__lhs < __rhs); }
1113 * @brief Tests the ordering of a string and a regular expression submatch.
1114 * @param __lhs A string.
1115 * @param __rhs A regular expression submatch.
1116 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1118 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1120 operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
1121 const sub_match<_Bi_iter>& __rhs)
1122 { return !(__rhs < __lhs); }
1125 * @brief Tests the equivalence of a regular expression submatch and a
1127 * @param __lhs A regular expression submatch.
1128 * @param __rhs A string.
1129 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1131 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1133 operator==(const sub_match<_Bi_iter>& __lhs,
1134 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1136 typedef typename sub_match<_Bi_iter>::string_type string_type;
1137 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) == 0;
1141 * @brief Tests the inequivalence of a regular expression submatch and a
1143 * @param __lhs A regular expression submatch.
1144 * @param __rhs A string.
1145 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1147 template<typename _Bi_iter, typename _Ch_traits, typename _Ch_alloc>
1149 operator!=(const sub_match<_Bi_iter>& __lhs,
1150 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1151 { return !(__lhs == __rhs); }
1154 * @brief Tests the ordering of a regular expression submatch and a string.
1155 * @param __lhs A regular expression submatch.
1156 * @param __rhs A string.
1157 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1159 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1161 operator<(const sub_match<_Bi_iter>& __lhs,
1162 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1164 typedef typename sub_match<_Bi_iter>::string_type string_type;
1165 return __lhs.compare(string_type(__rhs.data(), __rhs.size())) < 0;
1169 * @brief Tests the ordering of a regular expression submatch and a string.
1170 * @param __lhs A regular expression submatch.
1171 * @param __rhs A string.
1172 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1174 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1176 operator>(const sub_match<_Bi_iter>& __lhs,
1177 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1178 { return __rhs < __lhs; }
1181 * @brief Tests the ordering of a regular expression submatch and a string.
1182 * @param __lhs A regular expression submatch.
1183 * @param __rhs A string.
1184 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1186 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1188 operator>=(const sub_match<_Bi_iter>& __lhs,
1189 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1190 { return !(__lhs < __rhs); }
1193 * @brief Tests the ordering of a regular expression submatch and a string.
1194 * @param __lhs A regular expression submatch.
1195 * @param __rhs A string.
1196 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1198 template<typename _Bi_iter, class _Ch_traits, class _Ch_alloc>
1200 operator<=(const sub_match<_Bi_iter>& __lhs,
1201 const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs)
1202 { return !(__rhs < __lhs); }
1205 * @brief Tests the equivalence of a C string and a regular expression
1207 * @param __lhs A C string.
1208 * @param __rhs A regular expression submatch.
1209 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1211 template<typename _Bi_iter>
1213 operator==(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1214 const sub_match<_Bi_iter>& __rhs)
1215 { return __rhs.compare(__lhs) == 0; }
1218 * @brief Tests the inequivalence of an iterator value and a regular
1219 * expression submatch.
1220 * @param __lhs A regular expression submatch.
1221 * @param __rhs A string.
1222 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1224 template<typename _Bi_iter>
1226 operator!=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1227 const sub_match<_Bi_iter>& __rhs)
1228 { return !(__lhs == __rhs); }
1231 * @brief Tests the ordering of a string and a regular expression submatch.
1232 * @param __lhs A string.
1233 * @param __rhs A regular expression submatch.
1234 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1236 template<typename _Bi_iter>
1238 operator<(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1239 const sub_match<_Bi_iter>& __rhs)
1240 { return __rhs.compare(__lhs) > 0; }
1243 * @brief Tests the ordering of a string and a regular expression submatch.
1244 * @param __lhs A string.
1245 * @param __rhs A regular expression submatch.
1246 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1248 template<typename _Bi_iter>
1250 operator>(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1251 const sub_match<_Bi_iter>& __rhs)
1252 { return __rhs < __lhs; }
1255 * @brief Tests the ordering of a string and a regular expression submatch.
1256 * @param __lhs A string.
1257 * @param __rhs A regular expression submatch.
1258 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1260 template<typename _Bi_iter>
1262 operator>=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1263 const sub_match<_Bi_iter>& __rhs)
1264 { return !(__lhs < __rhs); }
1267 * @brief Tests the ordering of a string and a regular expression submatch.
1268 * @param __lhs A string.
1269 * @param __rhs A regular expression submatch.
1270 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1272 template<typename _Bi_iter>
1274 operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
1275 const sub_match<_Bi_iter>& __rhs)
1276 { return !(__rhs < __lhs); }
1279 * @brief Tests the equivalence of a regular expression submatch and a
1281 * @param __lhs A regular expression submatch.
1282 * @param __rhs A pointer to a string?
1283 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1285 template<typename _Bi_iter>
1287 operator==(const sub_match<_Bi_iter>& __lhs,
1288 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1289 { return __lhs.compare(__rhs) == 0; }
1292 * @brief Tests the inequivalence of a regular expression submatch and a
1294 * @param __lhs A regular expression submatch.
1295 * @param __rhs A pointer to a string.
1296 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1298 template<typename _Bi_iter>
1300 operator!=(const sub_match<_Bi_iter>& __lhs,
1301 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1302 { return !(__lhs == __rhs); }
1305 * @brief Tests the ordering of a regular expression submatch and a string.
1306 * @param __lhs A regular expression submatch.
1307 * @param __rhs A string.
1308 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1310 template<typename _Bi_iter>
1312 operator<(const sub_match<_Bi_iter>& __lhs,
1313 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1314 { return __lhs.compare(__rhs) < 0; }
1317 * @brief Tests the ordering of a regular expression submatch and a string.
1318 * @param __lhs A regular expression submatch.
1319 * @param __rhs A string.
1320 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1322 template<typename _Bi_iter>
1324 operator>(const sub_match<_Bi_iter>& __lhs,
1325 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1326 { return __rhs < __lhs; }
1329 * @brief Tests the ordering of a regular expression submatch and a string.
1330 * @param __lhs A regular expression submatch.
1331 * @param __rhs A string.
1332 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1334 template<typename _Bi_iter>
1336 operator>=(const sub_match<_Bi_iter>& __lhs,
1337 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1338 { return !(__lhs < __rhs); }
1341 * @brief Tests the ordering of a regular expression submatch and a string.
1342 * @param __lhs A regular expression submatch.
1343 * @param __rhs A string.
1344 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1346 template<typename _Bi_iter>
1348 operator<=(const sub_match<_Bi_iter>& __lhs,
1349 typename iterator_traits<_Bi_iter>::value_type const* __rhs)
1350 { return !(__rhs < __lhs); }
1353 * @brief Tests the equivalence of a string and a regular expression
1355 * @param __lhs A string.
1356 * @param __rhs A regular expression submatch.
1357 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1359 template<typename _Bi_iter>
1361 operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1362 const sub_match<_Bi_iter>& __rhs)
1364 typedef typename sub_match<_Bi_iter>::string_type string_type;
1365 return __rhs.compare(string_type(1, __lhs)) == 0;
1369 * @brief Tests the inequivalence of a string and a regular expression
1371 * @param __lhs A string.
1372 * @param __rhs A regular expression submatch.
1373 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1375 template<typename _Bi_iter>
1377 operator!=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1378 const sub_match<_Bi_iter>& __rhs)
1379 { return !(__lhs == __rhs); }
1382 * @brief Tests the ordering of a string and a regular expression submatch.
1383 * @param __lhs A string.
1384 * @param __rhs A regular expression submatch.
1385 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1387 template<typename _Bi_iter>
1389 operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1390 const sub_match<_Bi_iter>& __rhs)
1392 typedef typename sub_match<_Bi_iter>::string_type string_type;
1393 return __rhs.compare(string_type(1, __lhs)) > 0;
1397 * @brief Tests the ordering of a string and a regular expression submatch.
1398 * @param __lhs A string.
1399 * @param __rhs A regular expression submatch.
1400 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1402 template<typename _Bi_iter>
1404 operator>(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1405 const sub_match<_Bi_iter>& __rhs)
1406 { return __rhs < __lhs; }
1409 * @brief Tests the ordering of a string and a regular expression submatch.
1410 * @param __lhs A string.
1411 * @param __rhs A regular expression submatch.
1412 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1414 template<typename _Bi_iter>
1416 operator>=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1417 const sub_match<_Bi_iter>& __rhs)
1418 { return !(__lhs < __rhs); }
1421 * @brief Tests the ordering of a string and a regular expression submatch.
1422 * @param __lhs A string.
1423 * @param __rhs A regular expression submatch.
1424 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1426 template<typename _Bi_iter>
1428 operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
1429 const sub_match<_Bi_iter>& __rhs)
1430 { return !(__rhs < __lhs); }
1433 * @brief Tests the equivalence of a regular expression submatch and a
1435 * @param __lhs A regular expression submatch.
1436 * @param __rhs A const string reference.
1437 * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise.
1439 template<typename _Bi_iter>
1441 operator==(const sub_match<_Bi_iter>& __lhs,
1442 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1444 typedef typename sub_match<_Bi_iter>::string_type string_type;
1445 return __lhs.compare(string_type(1, __rhs)) == 0;
1449 * @brief Tests the inequivalence of a regular expression submatch and a
1451 * @param __lhs A regular expression submatch.
1452 * @param __rhs A const string reference.
1453 * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise.
1455 template<typename _Bi_iter>
1457 operator!=(const sub_match<_Bi_iter>& __lhs,
1458 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1459 { return !(__lhs == __rhs); }
1462 * @brief Tests the ordering of a regular expression submatch and a string.
1463 * @param __lhs A regular expression submatch.
1464 * @param __rhs A const string reference.
1465 * @returns true if @a __lhs precedes @a __rhs, false otherwise.
1467 template<typename _Bi_iter>
1469 operator<(const sub_match<_Bi_iter>& __lhs,
1470 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1472 typedef typename sub_match<_Bi_iter>::string_type string_type;
1473 return __lhs.compare(string_type(1, __rhs)) < 0;
1477 * @brief Tests the ordering of a regular expression submatch and a string.
1478 * @param __lhs A regular expression submatch.
1479 * @param __rhs A const string reference.
1480 * @returns true if @a __lhs succeeds @a __rhs, false otherwise.
1482 template<typename _Bi_iter>
1484 operator>(const sub_match<_Bi_iter>& __lhs,
1485 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1486 { return __rhs < __lhs; }
1489 * @brief Tests the ordering of a regular expression submatch and a string.
1490 * @param __lhs A regular expression submatch.
1491 * @param __rhs A const string reference.
1492 * @returns true if @a __lhs does not precede @a __rhs, false otherwise.
1494 template<typename _Bi_iter>
1496 operator>=(const sub_match<_Bi_iter>& __lhs,
1497 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1498 { return !(__lhs < __rhs); }
1501 * @brief Tests the ordering of a regular expression submatch and a string.
1502 * @param __lhs A regular expression submatch.
1503 * @param __rhs A const string reference.
1504 * @returns true if @a __lhs does not succeed @a __rhs, false otherwise.
1506 template<typename _Bi_iter>
1508 operator<=(const sub_match<_Bi_iter>& __lhs,
1509 typename iterator_traits<_Bi_iter>::value_type const& __rhs)
1510 { return !(__rhs < __lhs); }
1513 * @brief Inserts a matched string into an output stream.
1515 * @param __os The output stream.
1516 * @param __m A submatch string.
1518 * @returns the output stream with the submatch string inserted.
1520 template<typename _Ch_type, typename _Ch_traits, typename _Bi_iter>
1522 basic_ostream<_Ch_type, _Ch_traits>&
1523 operator<<(basic_ostream<_Ch_type, _Ch_traits>& __os,
1524 const sub_match<_Bi_iter>& __m)
1525 { return __os << __m.str(); }
1527 // [7.10] Class template match_results
1530 * @brief The results of a match or search operation.
1532 * A collection of character sequences representing the result of a regular
1533 * expression match. Storage for the collection is allocated and freed as
1534 * necessary by the member functions of class template match_results.
1536 * This class satisfies the Sequence requirements, with the exception that
1537 * only the operations defined for a const-qualified Sequence are supported.
1539 * The sub_match object stored at index 0 represents sub-expression 0, i.e.
1540 * the whole match. In this case the %sub_match member matched is always true.
1541 * The sub_match object stored at index n denotes what matched the marked
1542 * sub-expression n within the matched expression. If the sub-expression n
1543 * participated in a regular expression match then the %sub_match member
1544 * matched evaluates to true, and members first and second denote the range
1545 * of characters [first, second) which formed that match. Otherwise matched
1546 * is false, and members first and second point to the end of the sequence
1547 * that was searched.
1551 template<typename _Bi_iter,
1552 typename _Alloc = allocator<sub_match<_Bi_iter> > >
1554 : private std::vector<sub_match<_Bi_iter>, _Alloc>
1558 * The vector base is empty if this does not represent a match (!ready());
1559 * Otherwise if it's a match failure, it contains 3 elements:
1563 * Otherwise it contains n+4 elements where n is the number of marked
1566 * [1] 1st marked subexpression
1568 * [n] nth marked subexpression
1573 typedef std::vector<sub_match<_Bi_iter>, _Alloc> _Base_type;
1574 typedef std::iterator_traits<_Bi_iter> __iter_traits;
1575 typedef regex_constants::match_flag_type match_flag_type;
1579 * @name 10.? Public Types
1582 typedef sub_match<_Bi_iter> value_type;
1583 typedef const value_type& const_reference;
1584 typedef value_type& reference;
1585 typedef typename _Base_type::const_iterator const_iterator;
1586 typedef const_iterator iterator;
1587 typedef typename __iter_traits::difference_type difference_type;
1588 typedef typename allocator_traits<_Alloc>::size_type size_type;
1589 typedef _Alloc allocator_type;
1590 typedef typename __iter_traits::value_type char_type;
1591 typedef std::basic_string<char_type> string_type;
1596 * @name 28.10.1 Construction, Copying, and Destruction
1601 * @brief Constructs a default %match_results container.
1602 * @post size() returns 0 and str() returns an empty string.
1605 match_results(const _Alloc& __a = _Alloc())
1610 * @brief Copy constructs a %match_results.
1612 match_results(const match_results& __rhs) = default;
1615 * @brief Move constructs a %match_results.
1617 match_results(match_results&& __rhs) noexcept = default;
1620 * @brief Assigns rhs to *this.
1623 operator=(const match_results& __rhs) = default;
1626 * @brief Move-assigns rhs to *this.
1629 operator=(match_results&& __rhs) = default;
1632 * @brief Destroys a %match_results object.
1641 * @brief Indicates if the %match_results is ready.
1642 * @retval true The object has a fully-established result state.
1643 * @retval false The object is not ready.
1645 bool ready() const { return !_Base_type::empty(); }
1648 * @name 28.10.2 Size
1653 * @brief Gets the number of matches and submatches.
1655 * The number of matches for a given regular expression will be either 0
1656 * if there was no match or mark_count() + 1 if a match was successful.
1657 * Some matches may be empty.
1659 * @returns the number of matches found.
1663 { return _Base_type::empty() ? 0 : _Base_type::size() - 3; }
1667 { return _Base_type::max_size(); }
1670 * @brief Indicates if the %match_results contains no results.
1671 * @retval true The %match_results object is empty.
1672 * @retval false The %match_results object is not empty.
1676 { return size() == 0; }
1681 * @name 10.3 Element Access
1686 * @brief Gets the length of the indicated submatch.
1687 * @param __sub indicates the submatch.
1688 * @pre ready() == true
1690 * This function returns the length of the indicated submatch, or the
1691 * length of the entire match if @p __sub is zero (the default).
1694 length(size_type __sub = 0) const
1695 { return (*this)[__sub].length(); }
1698 * @brief Gets the offset of the beginning of the indicated submatch.
1699 * @param __sub indicates the submatch.
1700 * @pre ready() == true
1702 * This function returns the offset from the beginning of the target
1703 * sequence to the beginning of the submatch, unless the value of @p __sub
1704 * is zero (the default), in which case this function returns the offset
1705 * from the beginning of the target sequence to the beginning of the
1709 position(size_type __sub = 0) const
1710 { return std::distance(_M_begin, (*this)[__sub].first); }
1713 * @brief Gets the match or submatch converted to a string type.
1714 * @param __sub indicates the submatch.
1715 * @pre ready() == true
1717 * This function gets the submatch (or match, if @p __sub is
1718 * zero) extracted from the target range and converted to the
1719 * associated string type.
1722 str(size_type __sub = 0) const
1723 { return string_type((*this)[__sub]); }
1726 * @brief Gets a %sub_match reference for the match or submatch.
1727 * @param __sub indicates the submatch.
1728 * @pre ready() == true
1730 * This function gets a reference to the indicated submatch, or
1731 * the entire match if @p __sub is zero.
1733 * If @p __sub >= size() then this function returns a %sub_match with a
1734 * special value indicating no submatch.
1737 operator[](size_type __sub) const
1739 __glibcxx_assert( ready() );
1740 return __sub < size()
1741 ? _Base_type::operator[](__sub)
1742 : _M_unmatched_sub();
1746 * @brief Gets a %sub_match representing the match prefix.
1747 * @pre ready() == true
1749 * This function gets a reference to a %sub_match object representing the
1750 * part of the target range between the start of the target range and the
1751 * start of the match.
1756 __glibcxx_assert( ready() );
1757 return !empty() ? _M_prefix() : _M_unmatched_sub();
1761 * @brief Gets a %sub_match representing the match suffix.
1762 * @pre ready() == true
1764 * This function gets a reference to a %sub_match object representing the
1765 * part of the target range between the end of the match and the end of
1771 __glibcxx_assert( ready() );
1772 return !empty() ? _M_suffix() : _M_unmatched_sub();
1776 * @brief Gets an iterator to the start of the %sub_match collection.
1780 { return _Base_type::begin(); }
1783 * @brief Gets an iterator to the start of the %sub_match collection.
1787 { return this->begin(); }
1790 * @brief Gets an iterator to one-past-the-end of the collection.
1794 { return _Base_type::end() - (empty() ? 0 : 3); }
1797 * @brief Gets an iterator to one-past-the-end of the collection.
1801 { return this->end(); }
1806 * @name 10.4 Formatting
1808 * These functions perform formatted substitution of the matched
1809 * character sequences into their target. The format specifiers and
1810 * escape sequences accepted by these functions are determined by
1811 * their @p flags parameter as documented above.
1816 * @pre ready() == true
1818 template<typename _Out_iter>
1820 format(_Out_iter __out, const char_type* __fmt_first,
1821 const char_type* __fmt_last,
1822 match_flag_type __flags = regex_constants::format_default) const;
1825 * @pre ready() == true
1827 template<typename _Out_iter, typename _St, typename _Sa>
1829 format(_Out_iter __out, const basic_string<char_type, _St, _Sa>& __fmt,
1830 match_flag_type __flags = regex_constants::format_default) const
1832 return format(__out, __fmt.data(), __fmt.data() + __fmt.size(),
1837 * @pre ready() == true
1839 template<typename _St, typename _Sa>
1840 basic_string<char_type, _St, _Sa>
1841 format(const basic_string<char_type, _St, _Sa>& __fmt,
1842 match_flag_type __flags = regex_constants::format_default) const
1844 basic_string<char_type, _St, _Sa> __result;
1845 format(std::back_inserter(__result), __fmt, __flags);
1850 * @pre ready() == true
1853 format(const char_type* __fmt,
1854 match_flag_type __flags = regex_constants::format_default) const
1856 string_type __result;
1857 format(std::back_inserter(__result),
1859 __fmt + char_traits<char_type>::length(__fmt),
1867 * @name 10.5 Allocator
1872 * @brief Gets a copy of the allocator.
1875 get_allocator() const
1876 { return _Base_type::get_allocator(); }
1886 * @brief Swaps the contents of two match_results.
1889 swap(match_results& __that)
1892 _Base_type::swap(__that);
1893 swap(_M_begin, __that._M_begin);
1898 template<typename, typename, typename, bool>
1899 friend class __detail::_Executor;
1901 template<typename, typename, typename>
1902 friend class regex_iterator;
1904 template<typename _Bp, typename _Ap, typename _Cp, typename _Rp,
1905 __detail::_RegexExecutorPolicy, bool>
1907 __detail::__regex_algo_impl(_Bp, _Bp, match_results<_Bp, _Ap>&,
1908 const basic_regex<_Cp, _Rp>&,
1909 regex_constants::match_flag_type);
1912 _M_resize(unsigned int __size)
1913 { _Base_type::resize(__size + 3); }
1916 _M_unmatched_sub() const
1917 { return _Base_type::operator[](_Base_type::size() - 3); }
1919 sub_match<_Bi_iter>&
1921 { return _Base_type::operator[](_Base_type::size() - 3); }
1925 { return _Base_type::operator[](_Base_type::size() - 2); }
1927 sub_match<_Bi_iter>&
1929 { return _Base_type::operator[](_Base_type::size() - 2); }
1933 { return _Base_type::operator[](_Base_type::size() - 1); }
1935 sub_match<_Bi_iter>&
1937 { return _Base_type::operator[](_Base_type::size() - 1); }
1942 typedef match_results<const char*> cmatch;
1943 typedef match_results<string::const_iterator> smatch;
1944 #ifdef _GLIBCXX_USE_WCHAR_T
1945 typedef match_results<const wchar_t*> wcmatch;
1946 typedef match_results<wstring::const_iterator> wsmatch;
1949 // match_results comparisons
1951 * @brief Compares two match_results for equality.
1952 * @returns true if the two objects refer to the same match,
1955 template<typename _Bi_iter, typename _Alloc>
1957 operator==(const match_results<_Bi_iter, _Alloc>& __m1,
1958 const match_results<_Bi_iter, _Alloc>& __m2)
1960 if (__m1.ready() != __m2.ready())
1962 if (!__m1.ready()) // both are not ready
1964 if (__m1.empty() != __m2.empty())
1966 if (__m1.empty()) // both are empty
1968 return __m1.prefix() == __m2.prefix()
1969 && __m1.size() == __m2.size()
1970 && std::equal(__m1.begin(), __m1.end(), __m2.begin())
1971 && __m1.suffix() == __m2.suffix();
1975 * @brief Compares two match_results for inequality.
1976 * @returns true if the two objects do not refer to the same match,
1979 template<typename _Bi_iter, class _Alloc>
1981 operator!=(const match_results<_Bi_iter, _Alloc>& __m1,
1982 const match_results<_Bi_iter, _Alloc>& __m2)
1983 { return !(__m1 == __m2); }
1985 // [7.10.6] match_results swap
1987 * @brief Swaps two match results.
1988 * @param __lhs A match result.
1989 * @param __rhs A match result.
1991 * The contents of the two match_results objects are swapped.
1993 template<typename _Bi_iter, typename _Alloc>
1995 swap(match_results<_Bi_iter, _Alloc>& __lhs,
1996 match_results<_Bi_iter, _Alloc>& __rhs)
1997 { __lhs.swap(__rhs); }
1999 _GLIBCXX_END_NAMESPACE_CXX11
2001 // [7.11.2] Function template regex_match
2003 * @name Matching, Searching, and Replacing
2008 * @brief Determines if there is a match between the regular expression @p e
2009 * and all of the character sequence [first, last).
2011 * @param __s Start of the character sequence to match.
2012 * @param __e One-past-the-end of the character sequence to match.
2013 * @param __m The match results.
2014 * @param __re The regular expression.
2015 * @param __flags Controls how the regular expression is matched.
2017 * @retval true A match exists.
2018 * @retval false Otherwise.
2020 * @throws an exception of type regex_error.
2022 template<typename _Bi_iter, typename _Alloc,
2023 typename _Ch_type, typename _Rx_traits>
2025 regex_match(_Bi_iter __s,
2027 match_results<_Bi_iter, _Alloc>& __m,
2028 const basic_regex<_Ch_type, _Rx_traits>& __re,
2029 regex_constants::match_flag_type __flags
2030 = regex_constants::match_default)
2032 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2033 __detail::_RegexExecutorPolicy::_S_auto, true>
2034 (__s, __e, __m, __re, __flags);
2038 * @brief Indicates if there is a match between the regular expression @p e
2039 * and all of the character sequence [first, last).
2041 * @param __first Beginning of the character sequence to match.
2042 * @param __last One-past-the-end of the character sequence to match.
2043 * @param __re The regular expression.
2044 * @param __flags Controls how the regular expression is matched.
2046 * @retval true A match exists.
2047 * @retval false Otherwise.
2049 * @throws an exception of type regex_error.
2051 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2053 regex_match(_Bi_iter __first, _Bi_iter __last,
2054 const basic_regex<_Ch_type, _Rx_traits>& __re,
2055 regex_constants::match_flag_type __flags
2056 = regex_constants::match_default)
2058 match_results<_Bi_iter> __what;
2059 return regex_match(__first, __last, __what, __re, __flags);
2063 * @brief Determines if there is a match between the regular expression @p e
2064 * and a C-style null-terminated string.
2066 * @param __s The C-style null-terminated string to match.
2067 * @param __m The match results.
2068 * @param __re The regular expression.
2069 * @param __f Controls how the regular expression is matched.
2071 * @retval true A match exists.
2072 * @retval false Otherwise.
2074 * @throws an exception of type regex_error.
2076 template<typename _Ch_type, typename _Alloc, typename _Rx_traits>
2078 regex_match(const _Ch_type* __s,
2079 match_results<const _Ch_type*, _Alloc>& __m,
2080 const basic_regex<_Ch_type, _Rx_traits>& __re,
2081 regex_constants::match_flag_type __f
2082 = regex_constants::match_default)
2083 { return regex_match(__s, __s + _Rx_traits::length(__s), __m, __re, __f); }
2086 * @brief Determines if there is a match between the regular expression @p e
2089 * @param __s The string to match.
2090 * @param __m The match results.
2091 * @param __re The regular expression.
2092 * @param __flags Controls how the regular expression is matched.
2094 * @retval true A match exists.
2095 * @retval false Otherwise.
2097 * @throws an exception of type regex_error.
2099 template<typename _Ch_traits, typename _Ch_alloc,
2100 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2102 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2103 match_results<typename basic_string<_Ch_type,
2104 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2105 const basic_regex<_Ch_type, _Rx_traits>& __re,
2106 regex_constants::match_flag_type __flags
2107 = regex_constants::match_default)
2108 { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); }
2110 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2111 // 2329. regex_match() with match_results should forbid temporary strings
2112 /// Prevent unsafe attempts to get match_results from a temporary string.
2113 template<typename _Ch_traits, typename _Ch_alloc,
2114 typename _Alloc, typename _Ch_type, typename _Rx_traits>
2116 regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2117 match_results<typename basic_string<_Ch_type,
2118 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2119 const basic_regex<_Ch_type, _Rx_traits>&,
2120 regex_constants::match_flag_type
2121 = regex_constants::match_default) = delete;
2124 * @brief Indicates if there is a match between the regular expression @p e
2125 * and a C-style null-terminated string.
2127 * @param __s The C-style null-terminated string to match.
2128 * @param __re The regular expression.
2129 * @param __f Controls how the regular expression is matched.
2131 * @retval true A match exists.
2132 * @retval false Otherwise.
2134 * @throws an exception of type regex_error.
2136 template<typename _Ch_type, class _Rx_traits>
2138 regex_match(const _Ch_type* __s,
2139 const basic_regex<_Ch_type, _Rx_traits>& __re,
2140 regex_constants::match_flag_type __f
2141 = regex_constants::match_default)
2142 { return regex_match(__s, __s + _Rx_traits::length(__s), __re, __f); }
2145 * @brief Indicates if there is a match between the regular expression @p e
2148 * @param __s [IN] The string to match.
2149 * @param __re [IN] The regular expression.
2150 * @param __flags [IN] Controls how the regular expression is matched.
2152 * @retval true A match exists.
2153 * @retval false Otherwise.
2155 * @throws an exception of type regex_error.
2157 template<typename _Ch_traits, typename _Str_allocator,
2158 typename _Ch_type, typename _Rx_traits>
2160 regex_match(const basic_string<_Ch_type, _Ch_traits, _Str_allocator>& __s,
2161 const basic_regex<_Ch_type, _Rx_traits>& __re,
2162 regex_constants::match_flag_type __flags
2163 = regex_constants::match_default)
2164 { return regex_match(__s.begin(), __s.end(), __re, __flags); }
2166 // [7.11.3] Function template regex_search
2168 * Searches for a regular expression within a range.
2169 * @param __s [IN] The start of the string to search.
2170 * @param __e [IN] One-past-the-end of the string to search.
2171 * @param __m [OUT] The match results.
2172 * @param __re [IN] The regular expression to search for.
2173 * @param __flags [IN] Search policy flags.
2174 * @retval true A match was found within the string.
2175 * @retval false No match was found within the string, the content of %m is
2178 * @throws an exception of type regex_error.
2180 template<typename _Bi_iter, typename _Alloc,
2181 typename _Ch_type, typename _Rx_traits>
2183 regex_search(_Bi_iter __s, _Bi_iter __e,
2184 match_results<_Bi_iter, _Alloc>& __m,
2185 const basic_regex<_Ch_type, _Rx_traits>& __re,
2186 regex_constants::match_flag_type __flags
2187 = regex_constants::match_default)
2189 return __detail::__regex_algo_impl<_Bi_iter, _Alloc, _Ch_type, _Rx_traits,
2190 __detail::_RegexExecutorPolicy::_S_auto, false>
2191 (__s, __e, __m, __re, __flags);
2195 * Searches for a regular expression within a range.
2196 * @param __first [IN] The start of the string to search.
2197 * @param __last [IN] One-past-the-end of the string to search.
2198 * @param __re [IN] The regular expression to search for.
2199 * @param __flags [IN] Search policy flags.
2200 * @retval true A match was found within the string.
2201 * @retval false No match was found within the string.
2203 * @throws an exception of type regex_error.
2205 template<typename _Bi_iter, typename _Ch_type, typename _Rx_traits>
2207 regex_search(_Bi_iter __first, _Bi_iter __last,
2208 const basic_regex<_Ch_type, _Rx_traits>& __re,
2209 regex_constants::match_flag_type __flags
2210 = regex_constants::match_default)
2212 match_results<_Bi_iter> __what;
2213 return regex_search(__first, __last, __what, __re, __flags);
2217 * @brief Searches for a regular expression within a C-string.
2218 * @param __s [IN] A C-string to search for the regex.
2219 * @param __m [OUT] The set of regex matches.
2220 * @param __e [IN] The regex to search for in @p s.
2221 * @param __f [IN] The search flags.
2222 * @retval true A match was found within the string.
2223 * @retval false No match was found within the string, the content of %m is
2226 * @throws an exception of type regex_error.
2228 template<typename _Ch_type, class _Alloc, class _Rx_traits>
2230 regex_search(const _Ch_type* __s,
2231 match_results<const _Ch_type*, _Alloc>& __m,
2232 const basic_regex<_Ch_type, _Rx_traits>& __e,
2233 regex_constants::match_flag_type __f
2234 = regex_constants::match_default)
2235 { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }
2238 * @brief Searches for a regular expression within a C-string.
2239 * @param __s [IN] The C-string to search.
2240 * @param __e [IN] The regular expression to search for.
2241 * @param __f [IN] Search policy flags.
2242 * @retval true A match was found within the string.
2243 * @retval false No match was found within the string.
2245 * @throws an exception of type regex_error.
2247 template<typename _Ch_type, typename _Rx_traits>
2249 regex_search(const _Ch_type* __s,
2250 const basic_regex<_Ch_type, _Rx_traits>& __e,
2251 regex_constants::match_flag_type __f
2252 = regex_constants::match_default)
2253 { return regex_search(__s, __s + _Rx_traits::length(__s), __e, __f); }
2256 * @brief Searches for a regular expression within a string.
2257 * @param __s [IN] The string to search.
2258 * @param __e [IN] The regular expression to search for.
2259 * @param __flags [IN] Search policy flags.
2260 * @retval true A match was found within the string.
2261 * @retval false No match was found within the string.
2263 * @throws an exception of type regex_error.
2265 template<typename _Ch_traits, typename _String_allocator,
2266 typename _Ch_type, typename _Rx_traits>
2268 regex_search(const basic_string<_Ch_type, _Ch_traits,
2269 _String_allocator>& __s,
2270 const basic_regex<_Ch_type, _Rx_traits>& __e,
2271 regex_constants::match_flag_type __flags
2272 = regex_constants::match_default)
2273 { return regex_search(__s.begin(), __s.end(), __e, __flags); }
2276 * @brief Searches for a regular expression within a string.
2277 * @param __s [IN] A C++ string to search for the regex.
2278 * @param __m [OUT] The set of regex matches.
2279 * @param __e [IN] The regex to search for in @p s.
2280 * @param __f [IN] The search flags.
2281 * @retval true A match was found within the string.
2282 * @retval false No match was found within the string, the content of %m is
2285 * @throws an exception of type regex_error.
2287 template<typename _Ch_traits, typename _Ch_alloc,
2288 typename _Alloc, typename _Ch_type,
2289 typename _Rx_traits>
2291 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
2292 match_results<typename basic_string<_Ch_type,
2293 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
2294 const basic_regex<_Ch_type, _Rx_traits>& __e,
2295 regex_constants::match_flag_type __f
2296 = regex_constants::match_default)
2297 { return regex_search(__s.begin(), __s.end(), __m, __e, __f); }
2299 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2300 // 2329. regex_search() with match_results should forbid temporary strings
2301 /// Prevent unsafe attempts to get match_results from a temporary string.
2302 template<typename _Ch_traits, typename _Ch_alloc,
2303 typename _Alloc, typename _Ch_type,
2304 typename _Rx_traits>
2306 regex_search(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&,
2307 match_results<typename basic_string<_Ch_type,
2308 _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&,
2309 const basic_regex<_Ch_type, _Rx_traits>&,
2310 regex_constants::match_flag_type
2311 = regex_constants::match_default) = delete;
2313 // std [28.11.4] Function template regex_replace
2315 * @brief Search for a regular expression within a range for multiple times,
2316 and replace the matched parts through filling a format string.
2317 * @param __out [OUT] The output iterator.
2318 * @param __first [IN] The start of the string to search.
2319 * @param __last [IN] One-past-the-end of the string to search.
2320 * @param __e [IN] The regular expression to search for.
2321 * @param __fmt [IN] The format string.
2322 * @param __flags [IN] Search and replace policy flags.
2325 * @throws an exception of type regex_error.
2327 template<typename _Out_iter, typename _Bi_iter,
2328 typename _Rx_traits, typename _Ch_type,
2329 typename _St, typename _Sa>
2331 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2332 const basic_regex<_Ch_type, _Rx_traits>& __e,
2333 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2334 regex_constants::match_flag_type __flags
2335 = regex_constants::match_default)
2337 return regex_replace(__out, __first, __last, __e, __fmt.c_str(), __flags);
2341 * @brief Search for a regular expression within a range for multiple times,
2342 and replace the matched parts through filling a format C-string.
2343 * @param __out [OUT] The output iterator.
2344 * @param __first [IN] The start of the string to search.
2345 * @param __last [IN] One-past-the-end of the string to search.
2346 * @param __e [IN] The regular expression to search for.
2347 * @param __fmt [IN] The format C-string.
2348 * @param __flags [IN] Search and replace policy flags.
2351 * @throws an exception of type regex_error.
2353 template<typename _Out_iter, typename _Bi_iter,
2354 typename _Rx_traits, typename _Ch_type>
2356 regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
2357 const basic_regex<_Ch_type, _Rx_traits>& __e,
2358 const _Ch_type* __fmt,
2359 regex_constants::match_flag_type __flags
2360 = regex_constants::match_default);
2363 * @brief Search for a regular expression within a string for multiple times,
2364 and replace the matched parts through filling a format string.
2365 * @param __s [IN] The string to search and replace.
2366 * @param __e [IN] The regular expression to search for.
2367 * @param __fmt [IN] The format string.
2368 * @param __flags [IN] Search and replace policy flags.
2370 * @returns The string after replacing.
2371 * @throws an exception of type regex_error.
2373 template<typename _Rx_traits, typename _Ch_type,
2374 typename _St, typename _Sa, typename _Fst, typename _Fsa>
2375 inline basic_string<_Ch_type, _St, _Sa>
2376 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2377 const basic_regex<_Ch_type, _Rx_traits>& __e,
2378 const basic_string<_Ch_type, _Fst, _Fsa>& __fmt,
2379 regex_constants::match_flag_type __flags
2380 = regex_constants::match_default)
2382 basic_string<_Ch_type, _St, _Sa> __result;
2383 regex_replace(std::back_inserter(__result),
2384 __s.begin(), __s.end(), __e, __fmt, __flags);
2389 * @brief Search for a regular expression within a string for multiple times,
2390 and replace the matched parts through filling a format C-string.
2391 * @param __s [IN] The string to search and replace.
2392 * @param __e [IN] The regular expression to search for.
2393 * @param __fmt [IN] The format C-string.
2394 * @param __flags [IN] Search and replace policy flags.
2396 * @returns The string after replacing.
2397 * @throws an exception of type regex_error.
2399 template<typename _Rx_traits, typename _Ch_type,
2400 typename _St, typename _Sa>
2401 inline basic_string<_Ch_type, _St, _Sa>
2402 regex_replace(const basic_string<_Ch_type, _St, _Sa>& __s,
2403 const basic_regex<_Ch_type, _Rx_traits>& __e,
2404 const _Ch_type* __fmt,
2405 regex_constants::match_flag_type __flags
2406 = regex_constants::match_default)
2408 basic_string<_Ch_type, _St, _Sa> __result;
2409 regex_replace(std::back_inserter(__result),
2410 __s.begin(), __s.end(), __e, __fmt, __flags);
2415 * @brief Search for a regular expression within a C-string for multiple
2416 times, and replace the matched parts through filling a format string.
2417 * @param __s [IN] The C-string to search and replace.
2418 * @param __e [IN] The regular expression to search for.
2419 * @param __fmt [IN] The format string.
2420 * @param __flags [IN] Search and replace policy flags.
2422 * @returns The string after replacing.
2423 * @throws an exception of type regex_error.
2425 template<typename _Rx_traits, typename _Ch_type,
2426 typename _St, typename _Sa>
2427 inline basic_string<_Ch_type>
2428 regex_replace(const _Ch_type* __s,
2429 const basic_regex<_Ch_type, _Rx_traits>& __e,
2430 const basic_string<_Ch_type, _St, _Sa>& __fmt,
2431 regex_constants::match_flag_type __flags
2432 = regex_constants::match_default)
2434 basic_string<_Ch_type> __result;
2435 regex_replace(std::back_inserter(__result), __s,
2436 __s + char_traits<_Ch_type>::length(__s),
2437 __e, __fmt, __flags);
2442 * @brief Search for a regular expression within a C-string for multiple
2443 times, and replace the matched parts through filling a format C-string.
2444 * @param __s [IN] The C-string to search and replace.
2445 * @param __e [IN] The regular expression to search for.
2446 * @param __fmt [IN] The format C-string.
2447 * @param __flags [IN] Search and replace policy flags.
2449 * @returns The string after replacing.
2450 * @throws an exception of type regex_error.
2452 template<typename _Rx_traits, typename _Ch_type>
2453 inline basic_string<_Ch_type>
2454 regex_replace(const _Ch_type* __s,
2455 const basic_regex<_Ch_type, _Rx_traits>& __e,
2456 const _Ch_type* __fmt,
2457 regex_constants::match_flag_type __flags
2458 = regex_constants::match_default)
2460 basic_string<_Ch_type> __result;
2461 regex_replace(std::back_inserter(__result), __s,
2462 __s + char_traits<_Ch_type>::length(__s),
2463 __e, __fmt, __flags);
2469 _GLIBCXX_BEGIN_NAMESPACE_CXX11
2471 // std [28.12] Class template regex_iterator
2473 * An iterator adaptor that will provide repeated calls of regex_search over
2474 * a range until no more matches remain.
2476 template<typename _Bi_iter,
2477 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2478 typename _Rx_traits = regex_traits<_Ch_type> >
2479 class regex_iterator
2482 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2483 typedef match_results<_Bi_iter> value_type;
2484 typedef std::ptrdiff_t difference_type;
2485 typedef const value_type* pointer;
2486 typedef const value_type& reference;
2487 typedef std::forward_iterator_tag iterator_category;
2490 * @brief Provides a singular iterator, useful for indicating
2491 * one-past-the-end of a range.
2498 * Constructs a %regex_iterator...
2499 * @param __a [IN] The start of a text range to search.
2500 * @param __b [IN] One-past-the-end of the text range to search.
2501 * @param __re [IN] The regular expression to match.
2502 * @param __m [IN] Policy flags for match rules.
2504 regex_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2505 regex_constants::match_flag_type __m
2506 = regex_constants::match_default)
2507 : _M_begin(__a), _M_end(__b), _M_pregex(&__re), _M_flags(__m), _M_match()
2509 if (!regex_search(_M_begin, _M_end, _M_match, *_M_pregex, _M_flags))
2510 *this = regex_iterator();
2513 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2514 // 2332. regex_iterator should forbid temporary regexes
2515 regex_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2516 regex_constants::match_flag_type
2517 = regex_constants::match_default) = delete;
2519 * Copy constructs a %regex_iterator.
2521 regex_iterator(const regex_iterator& __rhs) = default;
2524 * @brief Assigns one %regex_iterator to another.
2527 operator=(const regex_iterator& __rhs) = default;
2530 * @brief Tests the equivalence of two regex iterators.
2533 operator==(const regex_iterator& __rhs) const;
2536 * @brief Tests the inequivalence of two regex iterators.
2539 operator!=(const regex_iterator& __rhs) const
2540 { return !(*this == __rhs); }
2543 * @brief Dereferences a %regex_iterator.
2547 { return _M_match; }
2550 * @brief Selects a %regex_iterator member.
2554 { return &_M_match; }
2557 * @brief Increments a %regex_iterator.
2563 * @brief Postincrements a %regex_iterator.
2576 const regex_type* _M_pregex;
2577 regex_constants::match_flag_type _M_flags;
2578 match_results<_Bi_iter> _M_match;
2581 typedef regex_iterator<const char*> cregex_iterator;
2582 typedef regex_iterator<string::const_iterator> sregex_iterator;
2583 #ifdef _GLIBCXX_USE_WCHAR_T
2584 typedef regex_iterator<const wchar_t*> wcregex_iterator;
2585 typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
2588 // [7.12.2] Class template regex_token_iterator
2590 * Iterates over submatches in a range (or @a splits a text string).
2592 * The purpose of this iterator is to enumerate all, or all specified,
2593 * matches of a regular expression within a text range. The dereferenced
2594 * value of an iterator of this class is a std::sub_match object.
2596 template<typename _Bi_iter,
2597 typename _Ch_type = typename iterator_traits<_Bi_iter>::value_type,
2598 typename _Rx_traits = regex_traits<_Ch_type> >
2599 class regex_token_iterator
2602 typedef basic_regex<_Ch_type, _Rx_traits> regex_type;
2603 typedef sub_match<_Bi_iter> value_type;
2604 typedef std::ptrdiff_t difference_type;
2605 typedef const value_type* pointer;
2606 typedef const value_type& reference;
2607 typedef std::forward_iterator_tag iterator_category;
2611 * @brief Default constructs a %regex_token_iterator.
2613 * A default-constructed %regex_token_iterator is a singular iterator
2614 * that will compare equal to the one-past-the-end value for any
2615 * iterator of the same type.
2617 regex_token_iterator()
2618 : _M_position(), _M_subs(), _M_suffix(), _M_n(0), _M_result(nullptr),
2623 * Constructs a %regex_token_iterator...
2624 * @param __a [IN] The start of the text to search.
2625 * @param __b [IN] One-past-the-end of the text to search.
2626 * @param __re [IN] The regular expression to search for.
2627 * @param __submatch [IN] Which submatch to return. There are some
2628 * special values for this parameter:
2629 * - -1 each enumerated subexpression does NOT
2630 * match the regular expression (aka field
2632 * - 0 the entire string matching the
2633 * subexpression is returned for each match
2635 * - >0 enumerates only the indicated
2636 * subexpression from a match within the text.
2637 * @param __m [IN] Policy flags for match rules.
2639 regex_token_iterator(_Bi_iter __a, _Bi_iter __b, const regex_type& __re,
2641 regex_constants::match_flag_type __m
2642 = regex_constants::match_default)
2643 : _M_position(__a, __b, __re, __m), _M_subs(1, __submatch), _M_n(0)
2644 { _M_init(__a, __b); }
2647 * Constructs a %regex_token_iterator...
2648 * @param __a [IN] The start of the text to search.
2649 * @param __b [IN] One-past-the-end of the text to search.
2650 * @param __re [IN] The regular expression to search for.
2651 * @param __submatches [IN] A list of subexpressions to return for each
2652 * regular expression match within the text.
2653 * @param __m [IN] Policy flags for match rules.
2655 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2656 const regex_type& __re,
2657 const std::vector<int>& __submatches,
2658 regex_constants::match_flag_type __m
2659 = regex_constants::match_default)
2660 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2661 { _M_init(__a, __b); }
2664 * Constructs a %regex_token_iterator...
2665 * @param __a [IN] The start of the text to search.
2666 * @param __b [IN] One-past-the-end of the text to search.
2667 * @param __re [IN] The regular expression to search for.
2668 * @param __submatches [IN] A list of subexpressions to return for each
2669 * regular expression match within the text.
2670 * @param __m [IN] Policy flags for match rules.
2672 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2673 const regex_type& __re,
2674 initializer_list<int> __submatches,
2675 regex_constants::match_flag_type __m
2676 = regex_constants::match_default)
2677 : _M_position(__a, __b, __re, __m), _M_subs(__submatches), _M_n(0)
2678 { _M_init(__a, __b); }
2681 * Constructs a %regex_token_iterator...
2682 * @param __a [IN] The start of the text to search.
2683 * @param __b [IN] One-past-the-end of the text to search.
2684 * @param __re [IN] The regular expression to search for.
2685 * @param __submatches [IN] A list of subexpressions to return for each
2686 * regular expression match within the text.
2687 * @param __m [IN] Policy flags for match rules.
2689 template<std::size_t _Nm>
2690 regex_token_iterator(_Bi_iter __a, _Bi_iter __b,
2691 const regex_type& __re,
2692 const int (&__submatches)[_Nm],
2693 regex_constants::match_flag_type __m
2694 = regex_constants::match_default)
2695 : _M_position(__a, __b, __re, __m),
2696 _M_subs(__submatches, __submatches + _Nm), _M_n(0)
2697 { _M_init(__a, __b); }
2699 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2700 // 2332. regex_token_iterator should forbid temporary regexes
2701 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&, int = 0,
2702 regex_constants::match_flag_type =
2703 regex_constants::match_default) = delete;
2704 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2705 const std::vector<int>&,
2706 regex_constants::match_flag_type =
2707 regex_constants::match_default) = delete;
2708 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2709 initializer_list<int>,
2710 regex_constants::match_flag_type =
2711 regex_constants::match_default) = delete;
2712 template <std::size_t _Nm>
2713 regex_token_iterator(_Bi_iter, _Bi_iter, const regex_type&&,
2715 regex_constants::match_flag_type =
2716 regex_constants::match_default) = delete;
2719 * @brief Copy constructs a %regex_token_iterator.
2720 * @param __rhs [IN] A %regex_token_iterator to copy.
2722 regex_token_iterator(const regex_token_iterator& __rhs)
2723 : _M_position(__rhs._M_position), _M_subs(__rhs._M_subs),
2724 _M_suffix(__rhs._M_suffix), _M_n(__rhs._M_n), _M_has_m1(__rhs._M_has_m1)
2725 { _M_normalize_result(); }
2728 * @brief Assigns a %regex_token_iterator to another.
2729 * @param __rhs [IN] A %regex_token_iterator to copy.
2731 regex_token_iterator&
2732 operator=(const regex_token_iterator& __rhs);
2735 * @brief Compares a %regex_token_iterator to another for equality.
2738 operator==(const regex_token_iterator& __rhs) const;
2741 * @brief Compares a %regex_token_iterator to another for inequality.
2744 operator!=(const regex_token_iterator& __rhs) const
2745 { return !(*this == __rhs); }
2748 * @brief Dereferences a %regex_token_iterator.
2752 { return *_M_result; }
2755 * @brief Selects a %regex_token_iterator member.
2759 { return _M_result; }
2762 * @brief Increments a %regex_token_iterator.
2764 regex_token_iterator&
2768 * @brief Postincrements a %regex_token_iterator.
2770 regex_token_iterator
2779 typedef regex_iterator<_Bi_iter, _Ch_type, _Rx_traits> _Position;
2782 _M_init(_Bi_iter __a, _Bi_iter __b);
2785 _M_current_match() const
2787 if (_M_subs[_M_n] == -1)
2788 return (*_M_position).prefix();
2790 return (*_M_position)[_M_subs[_M_n]];
2794 _M_end_of_seq() const
2795 { return _M_result == nullptr; }
2799 _M_normalize_result()
2801 if (_M_position != _Position())
2802 _M_result = &_M_current_match();
2804 _M_result = &_M_suffix;
2806 _M_result = nullptr;
2809 _Position _M_position;
2810 std::vector<int> _M_subs;
2811 value_type _M_suffix;
2813 const value_type* _M_result;
2815 // Show whether _M_subs contains -1
2819 /** @brief Token iterator for C-style NULL-terminated strings. */
2820 typedef regex_token_iterator<const char*> cregex_token_iterator;
2822 /** @brief Token iterator for standard strings. */
2823 typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
2825 #ifdef _GLIBCXX_USE_WCHAR_T
2826 /** @brief Token iterator for C-style NULL-terminated wide strings. */
2827 typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
2829 /** @brief Token iterator for standard wide-character strings. */
2830 typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;
2835 _GLIBCXX_END_NAMESPACE_CXX11
2836 _GLIBCXX_END_NAMESPACE_VERSION
2839 #include <bits/regex.tcc>