Regex Capture Groups

Splitting Strings with Regex

Can regex be used to split strings in C++?

Abstract art representing computer programming

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.

Using 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.

Splitting by More Complex Patterns

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.

Summary

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.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved