C++/따배C++ 18강 입력과 출력
[C++/CPP] 18.05 regular expressions, 정규 표현식
polarcompass
2021. 12. 19. 19:12
728x90
C++ 온라인 컴파일러 사이트
- https://replit.com/~
- https://www.onlinegdb.com/online_c++_compiler
- https://cpp.sh/
- 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