Namespaces
Variants

std:: regex_search

From cppreference.net
헤더 파일에 정의됨 <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 rvalue로 표현될 수 없습니다.

일치하는 항목이 존재하지 않는 경우, 다음 표현식들(존재한다면 m 을 포함하는)은 지정된 값을 반환해야 합니다:

표현식
m. ready ( ) true
m. size ( ) 0
m. empty ( ) true

매치가 존재하는 경우, ( 0 , m. size ( ) ) 범위 내의 임의의 정수를 n 으로 주었을 때, 아래 나열된 각 오버로드에 대해 m 을 포함하는 다음 표현식들은 지정된 값들을 반환해야 합니다:

Expression Value
오버로드 (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 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-terminated C-style 문자열
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 이 후조건에서 0일 수 있었음 양수만 가능함
LWG 2329 C++11 오버로드 (5) basic_string rvalue를 수용했으며,
이로 인해 댕글링 반복자가 발생할 수 있었음
삭제된 오버로드 (7) 를 통해 거부됨

참고 항목

정규 표현식 객체
(클래스 템플릿)
단일 정규 표현식 매치와 모든 하위 표현식 매치를 식별함
(클래스 템플릿)
정규 표현식을 전체 문자 시퀀스와 매치시키려 시도함
(함수 템플릿)