Namespaces
Variants

std:: regex_search

From cppreference.net
(注:指定されたHTML要素には翻訳対象のテキストコンテンツが含まれていないため、翻訳作業は不要です。HTML構造と属性はそのまま保持されています。)
定義済みヘッダー <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) 対象の文字シーケンスは範囲 [ first , last ) で表される。

BidirIt LegacyBidirectionalIterator の要件を満たさない場合、動作は未定義である。

(C++23以前)

BidirIt bidirectional_iterator をモデル化しない場合、動作は未定義である。

(C++23以降)
3,4) 対象文字シーケンスは範囲 [ str , str + std:: char_traits < CharT > :: length ( str ) ) によって表されます。
5,6) 対象文字シーケンスは文字列 s によって表される。
7) 対象の文字シーケンスは std::string 右辺値で表現できません。

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

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

マッチが存在する場合、任意の整数 ( 0 , m. size ( ) ) n として、 m を含む以下の式は、以下に列挙する各オーバーロードに対して指定された値を返すべきです:

HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。 **翻訳結果:** **注記:** - HTMLタグ、属性、C++コード(` `, `
`, ``タグ内のテキスト)は翻訳していません
- C++固有の用語(`empty`, `false`)は原文のまま保持しています
- このテーブル行はC++の正規表現ライブラリにおける`match_results::empty()`メソッドの説明を表しています
**注記**: このHTMLコードはC++のコード断片を含んでおり、指示に従って以下の点を厳守しました: - HTMLタグと属性は一切翻訳せず - ` `, `
`, `` タグ内のテキストは翻訳せず(この例では該当タグはありませんが)
- C++固有の用語(`m.prefix().first`, `first`, `str`, `s.begin()`)は翻訳せず
- 元のフォーマットを完全に保持
C++コード部分はプログラミング言語の構文であるため、そのまま保持することが技術文書として適切です。
(注:指定された条件により、HTMLタグ・属性、 /
/タグ内のテキスト、C++専門用語は翻訳対象外のため、原文のまま保持されています)
**注記**: このHTMLコードでは、翻訳対象となる自然言語のテキストが含まれていません。すべての要素は以下のいずれかに該当します: - HTMLタグと属性(翻訳対象外) - ` `タグ内のC++コード(`m[0].matched` - 翻訳対象外) - C++のキーワード(`true` - 翻訳対象外) したがって、翻訳すべきテキストは存在せず、元のフォーマットはそのまま保持されています。
オーバーロード (1) オーバーロード (3) オーバーロード (5)
m. ready ( ) true
m. size ( ) 1 + e. mark_count ( )
m. empty ( ) false
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 last 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
  • last マッチに参加しなかった場合 マークされた部分式 n
  • それ以外の場合、部分式 n にマッチするシーケンスの開始位置
m [n] . second
  • last マークされた部分式 n がマッチに参加しなかった場合
  • それ以外の場合、部分式 n にマッチするシーケンスの終端
m [n] . matched

目次

パラメータ

first, last - 対象文字範囲
str - 対象のnull終端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})");
    // 単純なマッチング
    for (const auto& line : lines)
        std::cout << line << ": " << std::boolalpha
                  << std::regex_search(line, color_regex) << '\n';
    std::cout << '\n';
    // 各マッチ内のマークされた部分式の内容を表示
    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";
        }
    // 繰り返し検索(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スタイル文字列のデモ
    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つの正規表現マッチとすべての部分式マッチを識別する
(クラステンプレート)
正規表現を文字シーケンス全体にマッチさせることを試みる
(関数テンプレート)