본문 바로가기
IT/C++.C

정규 표현식 예제1

by 골든크랩 2024. 2. 2.
728x90
반응형

 

설명은 나중에

 

#include <stdio.h>
#include <string>
#include <bits/stdc++.h>
#include <iostream>
#include <regex>  // ⭐⭐
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;


int main() {
   
    regex re("\\d");
    string str;

    while (true)
    {
        getline(cin, str);  
       
        //str : 검색 대상
        //re : 정규식
        if (std::regex_match(str, re))
            cout << "Match" << endl;
        else
            cout << "No Match" << endl;
       
        // 반복자를 이용하여 검사한 후 매치하는 부분만 출력
        {
            auto begin = std::sregex_iterator(str.begin(), str.end(), re);
            auto end = std::sregex_iterator();
            for (auto itr = begin; itr != end; ++itr)
            {
                std::smatch match = *itr;
                cout << match.str() << " ";
            }
            cout << endl;
        }
        cout << endl;
    }

    return 0;
}
728x90
반응형

댓글