名前空間
変種
操作

std::regex_search

From cppreference.com
< cpp‎ | regex
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
regex_search
(C++11)
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
ヘッダ <regex> で定義
template< class BidirIt, class Alloc, class CharT, class Traits >

bool regex_search( BidirIt first, BidirIt last,
                   std::match_results<BidirIt, Alloc>& m,
                   const std::basic_regex<CharT, Traits>& e,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(1) (C++11以降)
template< class BidirIt, class CharT, class Traits >

bool regex_search( BidirIt first, BidirIt last,
                   const std::basic_regex<CharT, Traits>& e,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(2) (C++11以降)
template< class CharT, class Alloc, class Traits >

bool regex_search( const CharT* str,
                   std::match_results<const CharT*, Alloc>& m,
                   const std::basic_regex<CharT, Traits>& e,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(3) (C++11以降)
template< class CharT, class Traits >

bool regex_search( const CharT* str, const std::basic_regex<CharT, Traits>& e,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(4) (C++11以降)
template< class STraits, class SAlloc, class Alloc,

          class CharT, class Traits >
bool regex_search
    ( const std::basic_string<CharT, STraits, SAlloc>& s,
      std::match_results
          <typename std::basic_string<CharT, STraits, SAlloc>::const_iterator,
           Alloc>& m,
      const std::basic_regex<CharT, Traits>& e,
      std::regex_constants::match_flag_type flags =

          std::regex_constants::match_default );
(5) (C++11以降)
template< class STraits, class SAlloc, class CharT, class Traits >

bool regex_search( const std::basic_string<CharT, STraits, SAlloc>& s,
                   const std::basic_regex<CharT, Traits>& e,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(6) (C++11以降)
template< class STraits, class SAlloc, class Alloc,

          class CharT, class Traits >
bool regex_search
    ( const std::basic_string<CharT, STraits, SAlloc>&&,
      std::match_results
          <typename std::basic_string<CharT, STraits, SAlloc>::const_iterator,
           Alloc>&,
      const std::basic_regex<CharT, Traits>&,
      std::regex_constants::match_flag_type flags =

          std::regex_constants::match_default ) = delete;
(7) (C++11以降)

正規表現eと、対象の文字シーケンス内のいずれかの部分シーケンスとの一致を決定します。詳細な一致結果は、(存在する場合) m に格納されます。

1,2) 対象の文字シーケンスは、範囲 [firstlast)で表されます。

BidirItLegacyBidirectionalIteratorの要件を満たさない場合、動作は未定義です。

(C++23まで)

BidirItbidirectional_iteratorをモデル化しない場合、動作は未定義です。

(C++23から)
3,4) 対象の文字シーケンスは、範囲 [strstr + std::char_traits<CharT>::length(str))で表されます。
5,6) 対象の文字シーケンスは、文字列 sで表されます。
7) 対象の文字シーケンスは、std::stringの右辺値では表せません。

一致が存在しない場合、m(存在する場合)に関連する以下の式は、指定された値を返すべきです。

 式   値 
m.ready() true
m.size() 0
m.empty() true

一致が存在する場合、m.size() の範囲 (0m.size()) 内の任意の整数を n として、以下の式は、以下にリストされた各オーバーロードについて、指定された値を返すべきです。

      式      
           オーバーロード (1)                       オーバーロード (3)                       オーバーロード (5)           
m.ready() true
m.size() 1 + e.mark_count()
m.empty() false
m.prefix().first first str s.begin()
m.prefix().second m[0].first
m.prefix().matched m.prefix().first != m.prefix().second
m.suffix().first m[0].second
m.suffix().second 最後 std::char_traits<CharT>::
    length(str) + str
s.end()
m.suffix().matched m.suffix().first != m.suffix().second
m[0].first eに一致したシーケンスの開始
m[0].second eに一致したシーケンスの終了
m[0].matched true
m[n].first
  • 一致しなかったマークされたサブ式 n の場合、last
  • それ以外の場合、サブ式 n に一致したシーケンスの開始
m[n].second
  • 一致しなかったマークされたサブ式 n の場合、last
  • それ以外の場合、サブ式 n に一致したシーケンスの終了
m[n].matched

目次

[編集] パラメータ

first, last - 対象の文字範囲
str - 対象のヌル終端Cスタイルの文字列
s - 対象のstd::basic_string
m - 一致結果
e - 正規表現
flags - 一致の実行方法を決定するために使用されるフラグ

[編集] 戻り値

一致が存在する場合はtrue、存在しない場合はfalseを返します。

[編集] 注意

対象シーケンス内のすべての一致を検査するには、std::regex_searchをループで呼び出し、各呼び出しごとに前の呼び出しのm[0].secondから再開することができます。std::regex_iteratorは、このイテレーションのための簡単なインターフェースを提供します。

[編集]

#include <cstddef>
#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::string lines[] = {"Roses are #ff0000",
                           "violets are #0000ff",
                           "all of my base are belong to you"};
 
    std::regex color_regex("#([a-f0-9]{2})"
                            "([a-f0-9]{2})"
                            "([a-f0-9]{2})");
 
    // simple match
    for (const auto& line : lines)
        std::cout << line << ": " << std::boolalpha
                  << std::regex_search(line, color_regex) << '\n';
    std::cout << '\n';
 
    // show contents of marked subexpressions within each match
    std::smatch color_match;
    for (const auto& line : lines)
        if (std::regex_search(line, color_match, color_regex))
        {
            std::cout << "matches for '" << line << "'\n";
            std::cout << "Prefix: '" << color_match.prefix() << "'\n";
            for (std::size_t i = 0; i < color_match.size(); ++i) 
                std::cout << i << ": " << color_match[i] << '\n';
            std::cout << "Suffix: '" << color_match.suffix() << "\'\n\n";
        }
 
    // repeated search (see also std::regex_iterator)
    std::string log(R"(
        Speed:	366
        Mass:	35
        Speed:	378
        Mass:	32
        Speed:	400
	Mass:	30)");
    std::regex r(R"(Speed:\t\d*)");
    for (std::smatch sm; regex_search(log, sm, r);)
    {
        std::cout << sm.str() << '\n';
        log = sm.suffix();
    }
 
    // C-style string demo
    std::cmatch cm;
    if (std::regex_search("this is a test", cm, std::regex("test"))) 
        std::cout << "\nFound " << cm[0] << " at position "
                  << cm.prefix().length() << '\n';
}

出力

Roses are #ff0000: true
violets are #0000ff: true
all of my base are belong to you: false
 
matches for 'Roses are #ff0000'
Prefix: 'Roses are '
0: #ff0000
1: ff
2: 00
3: 00
Suffix: ''
 
matches for 'violets are #0000ff'
Prefix: 'violets are '
0: #0000ff
1: 00
2: 00
3: ff
Suffix: ''
 
Speed:	366
Speed:	378
Speed:	400
 
Found test at position 10

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2205 C++11 事後条件におけるnはゼロになる可能性がある 正の値のみを許可
LWG 2329 C++11 オーバーロード (5)basic_stringの右辺値を受け入れていました。
これにより、無効なイテレータが生じる可能性がありました。
削除されたオーバーロード (7) により拒否されました。

[編集] 関連項目

正規表現オブジェクト
(クラステンプレート) [編集]
すべての部分式マッチを含む、1つの正規表現マッチを識別する
(クラステンプレート) [編集]
正規表現を文字シーケンス全体にマッチさせようと試みる
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)