Yes, you can use regex to split strings in C++. Although the C++ standard library does not provide a direct function for splitting strings using regex, you can achieve this by using std::regex_token_iterator
.
std::regex_token_iterator
The std::regex_token_iterator
allows you to iterate over matches of a regex pattern in a string, effectively letting you split the string based on a delimiter pattern.
Here’s how you can split a string by commas:
#include <iostream>
#include <regex>
#include <vector>
int main() {
std::string input = "apple,orange,banana";
std::regex pattern(",");
std::sregex_token_iterator it(
input.begin(), input.end(), pattern, -1);
std::sregex_token_iterator end;
std::vector<std::string> tokens;
while (it != end) {
tokens.push_back(*it++);
}
for (const auto& token : tokens) {
std::cout << token << "\n";
}
}
apple
orange
banana
In this example, the regex pattern ","
matches the comma delimiter. The -1
parameter tells std::regex_token_iterator
to split the string by the pattern rather than matching it.
You can split by more complex patterns. For instance, to split by one or more whitespace characters:
#include <iostream>
#include <regex>
#include <vector>
int main() {
std::string input = "apple orange banana";
std::regex pattern("\\s+");
std::sregex_token_iterator it(
input.begin(), input.end(), pattern, -1);
std::sregex_token_iterator end;
std::vector<std::string> tokens;
while (it != end) {
tokens.push_back(*it++);
}
for (const auto& token : tokens) {
std::cout << token << "\n";
}
}
apple
orange
banana
Here, the pattern "\s+"
matches one or more whitespace characters, allowing you to split the string based on spaces.
While C++ does not have a built-in function for splitting strings with regex, you can use std::regex_token_iterator
to achieve this. It provides a flexible way to split strings based on complex patterns, making it a powerful tool for text processing.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to regular expression capture groups, and how to use them in C++ with regex search
, replace
, iterator
, and token_iterator