250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/CPP] 18.05 regular expressions, 정규 표현식 본문

C++/따배C++ 18강 입력과 출력

[C++/CPP] 18.05 regular expressions, 정규 표현식

polarcompass 2021. 12. 19. 19:12
728x90
C++ 온라인 컴파일러 사이트
  1. https://replit.com/~
  2. https://www.onlinegdb.com/online_c++_compiler
  3. https://cpp.sh/
  4. https://www.tutorialspoint.com/compile_cpp_online.php
#include<iostream>
#include<string>
#include<vector>
#include<regex>

using namespace std;

int main()
{
    // regex re("\\d+"); // \d - digit인지?
    // regex re("[ab]");
    // regex re("[[:digit:]]{3}");
    // regex re("[A-Z]+");
    // regex re("[A-Z]{1,5}"); // 최소 1개, 최대 5개
    regex re("([0-9]{1})([-]?)([0-9]{1,4})");

    string str;

    while(true)
    {
        getline(cin, str);

        if (std::regex_match(str, re))
            cout << "Match" << endl;
        else
            cout << "No Match" << endl;
        
        // print matches
        {
            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