Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::regex_iterator

      From cppreference.com
      <cpp‎ |regex
       
       
       
      Regular expressions library
      Classes
      (C++11)
      Algorithms
      Iterators
      regex_iterator
      (C++11)
      Exceptions
      Traits
      Constants
      (C++11)
      Regex Grammar
       
       
      Defined in header<regex>
      template<

         class BidirIt,
         class CharT=typenamestd::iterator_traits<BidirIt>::value_type,
         class Traits=std::regex_traits<CharT>

      >class regex_iterator
      (since C++11)

      std::regex_iterator is a read-only iterator that accesses the individual matches of a regular expression within the underlying character sequence. It meets the requirements of aLegacyForwardIterator, except that for dereferenceable valuesa andb witha== b,*a and*b will not be bound to the same object.

      On construction, and on every increment, it callsstd::regex_search and remembers the result (that is, saves a copy of thestd::match_results<BidirIt> value). The first object may be read when the iterator is constructed or when the first dereferencing is done. Otherwise, dereferencing only returns a copy of the most recently obtained regex match.

      The default-constructedstd::regex_iterator is the end-of-sequence iterator. When a validstd::regex_iterator is incremented after reaching the last match (std::regex_search returnsfalse), it becomes equal to the end-of-sequence iterator. Dereferencing or incrementing it further invokes undefined behavior.

      A typical implementation ofstd::regex_iterator holds the begin and the end iterators for the underlying sequence (two instances ofBidirIt), a pointer to the regular expression (const regex_type*), the match flags (std::regex_constants::match_flag_type), and the current match (std::match_results<BidirIt>).

      Contents

      [edit]Type requirements

      -
      BidirIt must meet the requirements ofLegacyBidirectionalIterator.

      [edit]Specializations

      Several specializations for common character sequence types are defined:

      Defined in header<regex>
      Type Definition
      std::cregex_iteratorstd::regex_iterator<constchar*>
      std::wcregex_iteratorstd::regex_iterator<constwchar_t*>
      std::sregex_iteratorstd::regex_iterator<std::string::const_iterator>
      std::wsregex_iteratorstd::regex_iterator<std::wstring::const_iterator>

      [edit]Member types

      Type Definition
      value_typestd::match_results<BidirIt>
      difference_typestd::ptrdiff_t
      pointerconst value_type*
      referenceconst value_type&
      iterator_categorystd::forward_iterator_tag
      iterator_concept(C++20)std::input_iterator_tag
      regex_typestd::basic_regex<CharT, Traits>

      [edit]Data members

      Member Description
      BidiItbegin(private) the begin iterator
      (exposition-only member object*)
      BidiItend(private) the end iterator
      (exposition-only member object*)
      const regex_type*pregex(private) a pointer to a regular expression
      (exposition-only member object*)
      regex_constants::match_flag_typeflags(private) a flag
      (exposition-only member object*)
      match_results<BidiIt>match(private) the current match
      (exposition-only member object*)

      [edit]Member functions

      constructs a newregex_iterator
      (public member function)[edit]
      (destructor)
      (implicitly declared)
      destructs aregex_iterator, including the cached value
      (public member function)[edit]
      assigns contents
      (public member function)[edit]
      (removed in C++20)
      compares tworegex_iterators
      (public member function)[edit]
      accesses the current match
      (public member function)[edit]
      advances the iterator to the next match
      (public member function)[edit]

      [edit]Notes

      It is the programmer's responsibility to ensure that thestd::basic_regex object passed to the iterator's constructor outlives the iterator. Because the iterator stores a pointer to the regex, incrementing the iterator after the regex was destroyed accesses a dangling pointer.

      If the part of the regular expression that matched is just anassertion (^,$,\b,\B), the match stored in the iterator is a zero-length match, that is,match[0].first== match[0].second.

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <regex>#include <string> int main(){conststd::string s="Quick brown fox."; std::regex words_regex("[^\\s]+");auto words_begin= std::sregex_iterator(s.begin(), s.end(), words_regex);auto words_end= std::sregex_iterator(); std::cout<<"Found "<<std::distance(words_begin, words_end)<<" words:\n"; for(std::sregex_iterator i= words_begin; i!= words_end;++i){std::smatch match=*i;std::string match_str= match.str();std::cout<< match_str<<'\n';}}

      Output:

      Found 3 words:Quickbrownfox.

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3698
      (P2770R0)
      C++20regex_iterator was aforward_iterator
      while being a stashing iterator
      madeinput_iterator[1]
      1. iterator_category was unchanged by the resolution, because changing it tostd::input_iterator_tag might break too much existing code.

      [edit]See also

      identifies one regular expression match, including all sub-expression matches
      (class template)[edit]
      attempts to match a regular expression to any part of a character sequence
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/regex/regex_iterator&oldid=179535"

      [8]ページ先頭

      ©2009-2025 Movatter.jp