C++ preg_split() 구현

1 개요[ | ]

C++ preg_split() 구현
C++ 문자열 split() 구현
C++ 문자열 preg_split() 구현

2 aaa1bbb2ccc[ | ]

#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string line("aaa1bbb2ccc");    
    regex sep("[0-9]");
    sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ", "; // aaa, bbb, ccc, 
}

3 aaa111bbb222ccc[ | ]

#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string line("aaa111bbb222ccc");    
    regex sep("[0-9]+");
    sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ", "; // aaa, bbb, ccc, 
}
#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string line("aaa111bbb222ccc");    
    regex sep("[a-z]+");
    sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ", "; // , 111, 222,
}

4 123aaa[ | ]

#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string line("123aaa");
    regex sep("[0-9]");
    sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
    vector<string> tokens{iter, end};
    for (string t: tokens) cout << t << ", "; // , , , aaa, 
}
#include <iostream>
#include <vector>
#include <regex>
using namespace std;

int main() {
    string line("123aaa");
    regex sep("[0-9]");
    sregex_token_iterator iter(line.begin(), line.end(), sep, -1);
    auto tokens = vector<string>(iter, sregex_token_iterator());
    tokens.erase(remove_if(tokens.begin(), tokens.end(), [](string const& s){ return s.empty(); }), tokens.end());
    for (string t: tokens) cout << t << ", "; // aaa, 
}

5 abc,defgh,ijk[ | ]

#include <iostream>
#include <regex>
#include <vector>
using namespace std;

int main() {
    string in = "abc,defgh,ijk";
    
    regex sep(",");
    sregex_token_iterator iter(in.begin(), in.end(), sep, -1), end;
    vector<string> strs{iter, end};
    for (string s: strs) cout << s << ' '; // abc defgh ijk 
}
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
    string in = "abc,defgh,ijk";
    
    vector<string> strs;
    istringstream ss(in);
    for (string s; getline(ss, s, ','); ) {
        strs.push_back(s);
    }
    for (string s: strs) cout << s << ' '; // abc defgh ijk 
}

6 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}