Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Standard library header <regex> (C++11)

      From cppreference.com
      <cpp‎ |header
       
       
      Standard library headers
       

      This header is part of thetext processing library.

      Contents

      Includes

      (C++20)
      Three-way comparison operator support[edit]
      std::initializer_list class template[edit]

      Classes

      regular expression object
      (class template)[edit]
      std::regexstd::basic_regex<char>(typedef)
      std::wregexstd::basic_regex<wchar_t>(typedef)
      (C++11)
      identifies the sequence of characters matched by a sub-expression
      (class template)[edit]
      std::csub_matchstd::sub_match<constchar*>(typedef)
      std::wcsub_matchstd::sub_match<constwchar_t*>(typedef)
      std::ssub_matchstd::sub_match<std::string::const_iterator>(typedef)
      std::wssub_matchstd::sub_match<std::wstring::const_iterator>(typedef)
      identifies one regular expression match, including all sub-expression matches
      (class template)[edit]
      std::cmatchstd::match_results<constchar*>(typedef)
      std::wcmatchstd::match_results<constwchar_t*>(typedef)
      std::smatchstd::match_results<std::string::const_iterator>(typedef)
      std::wsmatchstd::match_results<std::wstring::const_iterator>(typedef)
      iterates through all regex matches within a character sequence
      (class template)[edit]
      std::cregex_iteratorstd::regex_iterator<constchar*>(typedef)
      std::wcregex_iteratorstd::regex_iterator<constwchar_t*>(typedef)
      std::sregex_iteratorstd::regex_iterator<std::string::const_iterator>(typedef)
      std::wsregex_iteratorstd::regex_iterator<std::wstring::const_iterator>(typedef)
      iterates through the specified sub-expressions within all regex matches in a given string or through unmatched substrings
      (class template)[edit]
      std::cregex_token_iteratorstd::regex_token_iterator<constchar*>(typedef)
      std::wcregex_token_iteratorstd::regex_token_iterator<constwchar_t*>(typedef)
      std::sregex_token_iteratorstd::regex_token_iterator<std::string::const_iterator>(typedef)
      std::wsregex_token_iteratorstd::regex_token_iterator<std::wstring::const_iterator>(typedef)
      reports errors generated by the regular expressions library
      (class)[edit]
      provides metainformation about a character type, required by the regex library
      (class template)[edit]
      regex constant types
      Defined in namespacestd::regex_constants
      general options controlling regex behavior
      (typedef)[edit]
      options specific to matching
      (typedef)[edit]
      (C++11)
      describes different types of matching errors
      (typedef)[edit]

      Functions

      Algorithm
      attempts to match a regular expression to an entire character sequence
      (function template)[edit]
      attempts to match a regular expression to any part of a character sequence
      (function template)[edit]
      replaces occurrences of a regular expression with formatted replacement text
      (function template)[edit]
      Non-member operations
      specializes thestd::swap algorithm
      (function template)[edit]
      (removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
      compares asub_match with anothersub_match, a string, or a character
      (function template)[edit]
      outputs the matched character subsequence
      (function template)[edit]
      (removed in C++20)
      lexicographically compares the values in the two match result
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]
      Range access
      (C++11)(C++14)
      returns an iterator to the beginning of a container or array
      (function template)[edit]
      (C++11)(C++14)
      returns an iterator to the end of a container or array
      (function template)[edit]
      returns a reverse iterator to the beginning of a container or array
      (function template)[edit]
      (C++14)
      returns a reverse end iterator for a container or array
      (function template)[edit]
      (C++17)(C++20)
      returns the size of a container or array
      (function template)[edit]
      (C++17)
      checks whether the container is empty
      (function template)[edit]
      (C++17)
      obtains the pointer to the underlying array
      (function template)[edit]

      [edit]Synopsis

      #include <compare>#include <initializer_list> namespace std{// regex constantsnamespace regex_constants{using syntax_option_type=/*T1*/;using match_flag_type=/*T2*/;using error_type=/*T3*/;} // class regex_errorclass regex_error; // class template regex_traitstemplate<class CharT>struct regex_traits; // class template basic_regextemplate<class CharT,class Traits= regex_traits<CharT>>class basic_regex; using regex= basic_regex<char>;using wregex= basic_regex<wchar_t>; // basic_regex swaptemplate<class CharT,class Traits>void swap(basic_regex<CharT, Traits>& e1, basic_regex<CharT, Traits>& e2); // class template sub_matchtemplate<class BiIt>class sub_match; using csub_match= sub_match<constchar*>;using wcsub_match= sub_match<constwchar_t*>;using ssub_match= sub_match<string::const_iterator>;using wssub_match= sub_match<wstring::const_iterator>; // sub_match non-member operatorstemplate<class BiIt>bool operator==(const sub_match<BiIt>& lhs,const sub_match<BiIt>& rhs);template<class BiIt>auto operator<=>(const sub_match<BiIt>& lhs,const sub_match<BiIt>& rhs); template<class BiIt,class ST,class SA>bool operator==(const sub_match<BiIt>& lhs,const basic_string<typename iterator_traits<BiIt>::value_type, ST, SA>& rhs);template<class BiIt,class ST,class SA>auto operator<=>(const sub_match<BiIt>& lhs,const basic_string<typename iterator_traits<BiIt>::value_type, ST, SA>& rhs); template<class BiIt>bool operator==(const sub_match<BiIt>& lhs,consttypename iterator_traits<BiIt>::value_type* rhs);template<class BiIt>auto operator<=>(const sub_match<BiIt>& lhs,consttypename iterator_traits<BiIt>::value_type* rhs); template<class BiIt>bool operator==(const sub_match<BiIt>& lhs,consttypename iterator_traits<BiIt>::value_type& rhs);template<class BiIt>auto operator<=>(const sub_match<BiIt>& lhs,consttypename iterator_traits<BiIt>::value_type& rhs); template<class CharT,class ST,class BiIt>    basic_ostream<CharT, ST>&      operator<<(basic_ostream<CharT, ST>& os,const sub_match<BiIt>& m); // class template match_resultstemplate<class BiIt,class Allocator= allocator<sub_match<BiIt>>>class match_results; using cmatch= match_results<constchar*>;using wcmatch= match_results<constwchar_t*>;using smatch= match_results<string::const_iterator>;using wsmatch= match_results<wstring::const_iterator>; // match_results comparisonstemplate<class BiIt,class Allocator>bool operator==(const match_results<BiIt, Allocator>& m1,const match_results<BiIt, Allocator>& m2); // match_results swaptemplate<class BiIt,class Allocator>void swap(match_results<BiIt, Allocator>& m1, match_results<BiIt, Allocator>& m2); // function template regex_matchtemplate<class BiIt,class Allocator,class CharT,class Traits>bool regex_match(BiIt first, BiIt last,                     match_results<BiIt, Allocator>& m,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default);template<class BiIt,class CharT,class Traits>bool regex_match(BiIt first, BiIt last,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default);template<class CharT,class Allocator,class Traits>bool regex_match(const CharT* str, match_results<const CharT*, Allocator>& m,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default);template<class ST,class SA,class Allocator,class CharT,class Traits>bool regex_match(const basic_string<CharT, ST, SA>& s,                     match_results<typename basic_string<CharT, ST, SA>::const_iterator,                                   Allocator>& m,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default);template<class ST,class SA,class Allocator,class CharT,class Traits>bool regex_match(const basic_string<CharT, ST, SA>&&,                     match_results<typename basic_string<CharT, ST, SA>::const_iterator,                                   Allocator>&,const basic_regex<CharT, Traits>&,                     regex_constants::match_flag_type=                       regex_constants::match_default)= delete;template<class CharT,class Traits>bool regex_match(const CharT* str,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default);template<class ST,class SA,class CharT,class Traits>bool regex_match(const basic_string<CharT, ST, SA>& s,const basic_regex<CharT, Traits>& e,                     regex_constants::match_flag_type flags=                       regex_constants::match_default); // function template regex_searchtemplate<class BiIt,class Allocator,class CharT,class Traits>bool regex_search(BiIt first, BiIt last,                      match_results<BiIt, Allocator>& m,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class BiIt,class CharT,class Traits>bool regex_search(BiItfirst, BiIt last,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class CharT,class Allocator,class Traits>bool regex_search(const CharT* str,                      match_results<const CharT*, Allocator>& m,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class CharT,class Traits>bool regex_search(const CharT* str,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class ST,class SA,class CharT,class Traits>bool regex_search(const basic_string<CharT, ST, SA>& s,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class ST,class SA,class Allocator,class CharT,class Traits>bool regex_search(const basic_string<CharT, ST, SA>& s,                      match_results<typename basic_string<CharT, ST, SA>::const_iterator,                                    Allocator>& m,const basic_regex<CharT, Traits>& e,                      regex_constants::match_flag_type flags=                        regex_constants::match_default);template<class ST,class SA,class Allocator,class CharT,class Traits>bool regex_search(const basic_string<CharT, ST, SA>&&,                      match_results<typename basic_string<CharT, ST, SA>::const_iterator,                                    Allocator>&,const basic_regex<CharT, Traits>&,                      regex_constants::match_flag_type= regex_constants::match_default)= delete; // function template regex_replacetemplate<class OutputIt,class BiIt,class Traits,class CharT,class ST,class SA>    OutputIt      regex_replace(OutputIt out,                    BiIt first, BiIt last,const basic_regex<CharT, Traits>& e,const basic_string<CharT, ST, SA>& fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default);template<class OutputIt,class BiIt,class Traits,class CharT>    OutputIt      regex_replace(OutputIt out,                    BiIt first, BiIt last,const basic_regex<CharT, Traits>& e,const CharT* fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default);template<class Traits,class CharT,class ST,class SA,class FST,class FSA>    basic_string<CharT, ST, SA>      regex_replace(const basic_string<CharT, ST, SA>& s,const basic_regex<CharT, Traits>& e,const basic_string<CharT, FST, FSA>& fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default);template<class Traits,class CharT,class ST,class SA>    basic_string<CharT, ST, SA>      regex_replace(const basic_string<CharT, ST, SA>& s,const basic_regex<CharT, Traits>& e,const CharT* fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default);template<class Traits,class CharT,class ST,class SA>    basic_string<CharT>      regex_replace(const CharT* s,const basic_regex<CharT, Traits>& e,const basic_string<CharT, ST, SA>& fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default);template<class Traits,class CharT>    basic_string<CharT>      regex_replace(const CharT* s,const basic_regex<CharT, Traits>& e,const CharT* fmt,                    regex_constants::match_flag_type flags=                      regex_constants::match_default); // class template regex_iteratortemplate<class BiIt,class CharT=typename iterator_traits<BiIt>::value_type,class Traits= regex_traits<CharT>>class regex_iterator; using cregex_iterator= regex_iterator<constchar*>;using wcregex_iterator= regex_iterator<constwchar_t*>;using sregex_iterator= regex_iterator<string::const_iterator>;using wsregex_iterator= regex_iterator<wstring::const_iterator>; // class template regex_token_iteratortemplate<class BiIt,class CharT=typename iterator_traits<BiIt>::value_type,class Traits= regex_traits<CharT>>class regex_token_iterator; using cregex_token_iterator= regex_token_iterator<constchar*>;using wcregex_token_iterator= regex_token_iterator<constwchar_t*>;using sregex_token_iterator= regex_token_iterator<string::const_iterator>;using wsregex_token_iterator= regex_token_iterator<wstring::const_iterator>; namespace pmr{template<class BiIt>using match_results=std::match_results<BiIt, polymorphic_allocator<sub_match<BiIt>>>; using cmatch= match_results<constchar*>;using wcmatch= match_results<constwchar_t*>;using smatch= match_results<string::const_iterator>;using wsmatch= match_results<wstring::const_iterator>;}}

      [edit]Bitmask typestd::regex_constants::syntax_option_type

      namespace std::regex_constants{using syntax_option_type=/*T1*/;inlineconstexpr syntax_option_type icase=/* unspecified */;inlineconstexpr syntax_option_type nosubs=/* unspecified */;inlineconstexpr syntax_option_type optimize=/* unspecified */;inlineconstexpr syntax_option_type collate=/* unspecified */;inlineconstexpr syntax_option_type ECMAScript=/* unspecified */;inlineconstexpr syntax_option_type basic=/* unspecified */;inlineconstexpr syntax_option_type extended=/* unspecified */;inlineconstexpr syntax_option_type awk=/* unspecified */;inlineconstexpr syntax_option_type grep=/* unspecified */;inlineconstexpr syntax_option_type egrep=/* unspecified */;inlineconstexpr syntax_option_type multiline=/* unspecified */;}

      [edit]Bitmask typestd::regex_constants::match_flag_type

      namespace std::regex_constants{using match_flag_type=/*T2*/;inlineconstexpr match_flag_type match_default={};inlineconstexpr match_flag_type match_not_bol=/* unspecified */;inlineconstexpr match_flag_type match_not_eol=/* unspecified */;inlineconstexpr match_flag_type match_not_bow=/* unspecified */;inlineconstexpr match_flag_type match_not_eow=/* unspecified */;inlineconstexpr match_flag_type match_any=/* unspecified */;inlineconstexpr match_flag_type match_not_null=/* unspecified */;inlineconstexpr match_flag_type match_continuous=/* unspecified */;inlineconstexpr match_flag_type match_prev_avail=/* unspecified */;inlineconstexpr match_flag_type format_default={};inlineconstexpr match_flag_type format_sed=/* unspecified */;inlineconstexpr match_flag_type format_no_copy=/* unspecified */;inlineconstexpr match_flag_type format_first_only=/* unspecified */;}

      [edit]Enumerated typestd::regex_constants::error_type

      namespace std::regex_constants{using error_type=/*T3*/;inlineconstexpr error_type error_collate=/* unspecified */;inlineconstexpr error_type error_ctype=/* unspecified */;inlineconstexpr error_type error_escape=/* unspecified */;inlineconstexpr error_type error_backref=/* unspecified */;inlineconstexpr error_type error_brack=/* unspecified */;inlineconstexpr error_type error_paren=/* unspecified */;inlineconstexpr error_type error_brace=/* unspecified */;inlineconstexpr error_type error_badbrace=/* unspecified */;inlineconstexpr error_type error_range=/* unspecified */;inlineconstexpr error_type error_space=/* unspecified */;inlineconstexpr error_type error_badrepeat=/* unspecified */;inlineconstexpr error_type error_complexity=/* unspecified */;inlineconstexpr error_type error_stack=/* unspecified */;}

      [edit]Classstd::regex_error

      namespace std{class regex_error:public runtime_error{public:explicit regex_error(regex_constants::error_type ecode);    regex_constants::error_type code()const;};}

      [edit]Class templatestd::regex_traits

      namespace std{template<class CharT>struct regex_traits{using char_type= CharT;using string_type= basic_string<char_type>;using locale_type= locale;using char_class_type=/* bitmask-type */;       regex_traits();static size_t length(const char_type* p);      CharT translate(CharT c)const;      CharT translate_nocase(CharT c)const;template<class ForwardIt>        string_type transform(ForwardIt first, ForwardIt last)const;template<class ForwardIt>        string_type transform_primary(ForwardIt first, ForwardIt last)const;template<class ForwardIt>        string_type lookup_collatename(ForwardIt first, ForwardIt last)const;template<class ForwardIt>        char_class_type lookup_classname(ForwardIt first, ForwardIt last,bool icase=false)const;bool isctype(CharT c, char_class_type f)const;int value(CharT ch,int radix)const;      locale_type imbue(locale_type l);      locale_type getloc()const;};}

      [edit]Class templatestd::basic_regex

      namespace std{template<class CharT,class Traits= regex_traits<CharT>>class basic_regex{public:// typesusing value_type=          CharT;using Traits_type=          Traits;using string_type=typename Traits::string_type;using flag_type=          regex_constants::syntax_option_type;using locale_type=typename Traits::locale_type; // constantsstaticconstexpr flag_type icase= regex_constants::icase;staticconstexpr flag_type nosubs= regex_constants::nosubs;staticconstexpr flag_type optimize= regex_constants::optimize;staticconstexpr flag_type collate= regex_constants::collate;staticconstexpr flag_type ECMAScript= regex_constants::ECMAScript;staticconstexpr flag_type basic= regex_constants::basic;staticconstexpr flag_type extended= regex_constants::extended;staticconstexpr flag_type awk= regex_constants::awk;staticconstexpr flag_type grep= regex_constants::grep;staticconstexpr flag_type egrep= regex_constants::egrep;staticconstexpr flag_type multiline= regex_constants::multiline; // construct/copy/destroy      basic_regex();explicit basic_regex(const CharT* p, flag_type f= regex_constants::ECMAScript);      basic_regex(const CharT* p, size_t len, flag_type f= regex_constants::ECMAScript);      basic_regex(const basic_regex&);      basic_regex(basic_regex&&)noexcept;template<class ST,class SA>explicit basic_regex(const basic_string<CharT, ST, SA>& s,                             flag_type f= regex_constants::ECMAScript);template<class ForwardIt>        basic_regex(ForwardIt first, ForwardIt last,                    flag_type f= regex_constants::ECMAScript);      basic_regex(initializer_list<CharT> il, flag_type f= regex_constants::ECMAScript);       ~basic_regex(); // assign      basic_regex& operator=(const basic_regex& e);      basic_regex& operator=(basic_regex&& e)noexcept;      basic_regex& operator=(const CharT* p);      basic_regex& operator=(initializer_list<CharT> il);template<class ST,class SA>        basic_regex& operator=(const basic_string<CharT, ST, SA>& s);       basic_regex& assign(const basic_regex& e);      basic_regex& assign(basic_regex&& e)noexcept;      basic_regex& assign(const CharT* p, flag_type f= regex_constants::ECMAScript);      basic_regex& assign(const CharT* p, size_t len,                          flag_type f= regex_constants::ECMAScript);template<class ST,class SA>        basic_regex& assign(const basic_string<CharT, ST, SA>& s,                            flag_type f= regex_constants::ECMAScript);template<class InputIt>        basic_regex& assign(InputIt first, InputIt last,                            flag_type f= regex_constants::ECMAScript);      basic_regex& assign(initializer_list<CharT>,                          flag_type f= regex_constants::ECMAScript); // const operationsunsigned mark_count()const;      flag_type flags()const; // locale      locale_type imbue(locale_type loc);      locale_type getloc()const; // swapvoid swap(basic_regex&);}; template<class ForwardIt>    basic_regex(ForwardIt, ForwardIt,                regex_constants::syntax_option_type= regex_constants::ECMAScript)-> basic_regex<typename iterator_traits<ForwardIt>::value_type>;}

      [edit]Class templatestd::sub_match

      namespace std{template<class BiIt>class sub_match:public pair<BiIt, BiIt>{public:using value_type=typename iterator_traits<BiIt>::value_type;using difference_type=typename iterator_traits<BiIt>::difference_type;using iterator= BiIt;using string_type= basic_string<value_type>; bool matched; constexpr sub_match();       difference_type length()const;      operator string_type()const;      string_type str()const; int compare(const sub_match& s)const;int compare(const string_type& s)const;int compare(const value_type* s)const; void swap(sub_match& s)noexcept(/* see description */);};}

      [edit]Class templatestd::match_results

      namespace std{template<class BiIt,class Allocator= allocator<sub_match<BiIt>>>class match_results{public:using value_type= sub_match<BiIt>;using const_reference=const value_type&;using reference= value_type&;using const_iterator=/* implementation-defined */;using iterator= const_iterator;using difference_type=typename iterator_traits<BiIt>::difference_type;using size_type=typename allocator_traits<Allocator>::size_type;using allocator_type= Allocator;using char_type=typename iterator_traits<BiIt>::value_type;using string_type= basic_string<char_type>; // construct/copy/destroy      match_results(): match_results(Allocator()){}explicit match_results(const Allocator& a);      match_results(const match_results& m);      match_results(const match_results& m,const Allocator& a);      match_results(match_results&& m)noexcept;      match_results(match_results&& m,const Allocator& a);      match_results& operator=(const match_results& m);      match_results& operator=(match_results&& m);      ~match_results(); // statebool ready()const; // size      size_type size()const;      size_type max_size()const;bool empty()const; // element access      difference_type length(size_type sub=0)const;      difference_type position(size_type sub=0)const;      string_type str(size_type sub=0)const;      const_reference operator[](size_type n)const;       const_reference prefix()const;      const_reference suffix()const;      const_iterator begin()const;      const_iterator end()const;      const_iterator cbegin()const;      const_iterator cend()const; // formattemplate<class OutputIt>        OutputIt          format(OutputIt out,const char_type* fmt_first,const char_type* fmt_last,                 regex_constants::match_flag_type flags=                   regex_constants::format_default)const;template<class OutputIt,class ST,class SA>        OutputIt          format(OutputIt out,const basic_string<char_type, ST, SA>& fmt,                 regex_constants::match_flag_type flags=                   regex_constants::format_default)const;template<class ST,class SA>        basic_string<char_type, ST, SA>          format(const basic_string<char_type, ST, SA>& fmt,                 regex_constants::match_flag_type flags=                   regex_constants::format_default)const;      string_type        format(const char_type* fmt,               regex_constants::match_flag_type flags=                 regex_constants::format_default)const; // allocator      allocator_type get_allocator()const; // swapvoid swap(match_results& that);};}
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/header/regex&oldid=179092"

      [8]ページ先頭

      ©2009-2025 Movatter.jp