"C++ preg split() 구현"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
{{DISPLAYTITLE:C++ 문자열 preg_split() 구현}}
{{DISPLAYTITLE:C++ preg_split() 구현}}
;C++ preg_split() 구현
;C++ preg_split() 구현
;C++ 문자열 split() 구현
;C++ 문자열 split() 구현
14번째 줄: 14번째 줄:
int main() {
int main() {
     string line("aaa1bbb2ccc");     
     string line("aaa1bbb2ccc");     
     regex rx("[0-9]");
     regex sep("[0-9]");
     sregex_token_iterator iter(line.begin(), line.end(), rx, -1), end;
     sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
     vector<string> strs{iter, end};
     vector<string> strs{iter, end};
     for (string s: strs) cout << s << ", "; // aaa, bbb, ccc,  
     for (string s: strs) cout << s << ", "; // aaa, bbb, ccc,  
}
</syntaxhighlight>
==aaa111bbb222ccc==
<syntaxhighlight lang='cpp' run>
#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,
}
</syntaxhighlight>
<syntaxhighlight lang='cpp' run>
#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,
}
}
</syntaxhighlight>
</syntaxhighlight>
30번째 줄: 60번째 줄:
int main() {
int main() {
     string line("123aaa");
     string line("123aaa");
     regex seps("[0-9]");
     regex sep("[0-9]");
     sregex_token_iterator iter(line.begin(), line.end(), seps, -1), end;
     sregex_token_iterator iter(line.begin(), line.end(), sep, -1), end;
     vector<string> tokens{iter, end};
     vector<string> tokens{iter, end};
     for (string t: tokens) cout << t << ", "; // , , , aaa,  
     for (string t: tokens) cout << t << ", "; // , , , aaa,  
44번째 줄: 74번째 줄:
int main() {
int main() {
     string line("123aaa");
     string line("123aaa");
     regex seps("[0-9]");
     regex sep("[0-9]");
     sregex_token_iterator iter(line.begin(), line.end(), seps, -1);
     sregex_token_iterator iter(line.begin(), line.end(), sep, -1);
     auto tokens = vector<string>(iter, sregex_token_iterator());
     auto tokens = vector<string>(iter, sregex_token_iterator());
     tokens.erase(remove_if(tokens.begin(), tokens.end(), [](string const& s){ return s.empty(); }), tokens.end());
     tokens.erase(remove_if(tokens.begin(), tokens.end(), [](string const& s){ return s.empty(); }), tokens.end());
62번째 줄: 92번째 줄:
     string in = "abc,defgh,ijk";
     string in = "abc,defgh,ijk";
      
      
     regex rx(",");
     regex sep(",");
     sregex_token_iterator iter(in.begin(), in.end(), rx, -1), end;
     sregex_token_iterator iter(in.begin(), in.end(), sep, -1), end;
     vector<string> strs{iter, end};
     vector<string> strs{iter, end};
     for (string s: strs) cout << s << ' '; // abc defgh ijk  
     for (string s: strs) cout << s << ' '; // abc defgh ijk  
88번째 줄: 118번째 줄:
==같이 보기==
==같이 보기==
* [[C++ 문자열]]
* [[C++ 문자열]]
* [[C++ preg_split_delim_capture() 구현]]
* [[함수 preg_split()]]
* [[함수 preg_split()]]



2023년 11월 10일 (금) 18:25 기준 최신판

1 개요[ | ]

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

2 aaa1bbb2ccc[ | ]

C++
Copy
#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, 
}
Loading

3 aaa111bbb222ccc[ | ]

C++
Copy
#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, 
}
Loading
C++
Copy
#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,
}
Loading

4 123aaa[ | ]

C++
Copy
#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, 
}
Loading
C++
Copy
#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, 
}
Loading

5 abc,defgh,ijk[ | ]

C++
Copy
#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 
}
Loading
C++
Copy
#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 
}
Loading

6 같이 보기[ | ]