Groups and ranges (그룹과 범위)
Quantifiers (수량)
Boundary
Character classes
Notice that, in C++, character and string literals also escape characters using the backslash character (\), and this affects the syntax for constructing regular expressions from such types. For example:
1 std::regex e1 ("\\d"); // regular expression: \d -> matches a digit character
2 std::regex e2 ("\\\\"); // regular expression: \\ -> matches a single backslash (\) character
#include <iostream>
#include <string>
#include <regex>
using namespace std;
vector<string> getMatches(string str, regex reg)
{
vector<string> matches;
// sregex_iterator 내, begin(), end(), reg를 저장하여 순회 가능
sregex_iterator curMatch(str.begin(), str.end(), reg);
// lastMatch는 end로 초기화됨
sregex_iterator lastMatch;
while (curMatch != lastMatch) {
smatch match = *curMatch;
matches.push_back(match.str());
curMatch++;
}
// match.str() // match된 string
// match.prefix() // match된 string 앞 부분
// match.suffix() // match된 string 뒷 부분
return matches;
}
vector<string> getCaptures(string str, regex reg)
{
vector<string> matches;
// sregex_iterator 내, begin(), end(), reg를 저장하여 순회 가능
sregex_iterator curMatch(str.begin(), str.end(), reg);
// lastMatch는 end로 초기화됨
sregex_iterator lastMatch;
while (curMatch != lastMatch) {
smatch match = *curMatch;
matches.push_back(match[1]);
curMatch++;
}
// match.str() // match된 string
// match.prefix() // match된 string 앞 부분
// match.suffix() // match된 string 뒷 부분
return matches;
}
int main()
{
string str = "\
Hi there, Nice to meet you. And Hello there and hi.\n\
I love grey(gray) color not a gry, graayand graaay.grdy\n\
Ya ya YaYaYa Ya\n\
abcdefghijklmnopqrstuvwxyz\n\
ABSCEFGHIJKLMNOPQRSTUVWZYZ\n\
1234567890\n\
.[]{}()\^$|?*+\n\
010-898-0893\n\
010 898 0893\n\
010.898.0893\n\
010-405-3412\n\
02-878-8888\n\
dream.coder.ellie@gmail.com\n\
hel+lo@daum.net\n\
hello@daum.co.kr\n\
...\n\
http://www.youtu.be/-ZClicWm0zM\n\
https://www.youtu.be/-ZClicWm1zM\n\
https://youtu.be/-ZClicWm2zM\n\
youtu.be/-ZClicWm3zM";
regex reg1("\\d{2,3}[- .]\\d{3}[- .]\\d{4}"); // 전화번호 검색
regex reg2("[\\w+-._]+@[\\w+-._]+"); // 이메일 검색
regex reg3("(?:https?:\\/\\/)?(?:www\\.)?youtu\\.be\/([a-zA-Z0-9-]+)"); // 유튜브에서 ID만 가져오기...
//vector<string> matches = getMatches(str, reg3);
vector<string> matches = getCaptures(str, reg3);
for (int i = 0; i < matches.size(); i++) {
cout << i << " : " << matches[i] << endl;
}
return 0;
}