C++ std::regex_replace šŸŽÆ

beginner
25 min

C++ std::regex_replace šŸŽÆ

Welcome to our deep dive into the world of C++'s powerful string manipulation tool, std::regex_replace! This lesson is designed for both beginners and intermediates, so let's get started!

Introduction šŸ“

std::regex_replace is a function in the C++ Standard Library that lets you replace parts of a string according to a regular expression pattern. It's a versatile tool that can help you perform complex text transformations in your code.

Getting Started šŸ’”

Before we dive into std::regex_replace, let's briefly review the basics of strings and regular expressions in C++.

Strings in C++ šŸ“

In C++, strings are handled as std::string objects. They can be declared, assigned, and manipulated like any other variable.

cpp
#include <string> int main() { std::string my_string = "Hello, World!"; std::cout << my_string << std::endl; return 0; }

Regular Expressions šŸ’”

Regular expressions (regex) are a powerful tool for pattern matching in strings. They allow you to match, capture, and replace patterns in text. In C++, we use the <regex> header to work with regular expressions.

Using std::regex_replace šŸ’”

Now, let's dive into std::regex_replace. The function takes four arguments:

  1. The input string
  2. The regular expression pattern
  3. The replacement string
  4. The std::match_flags which control how the pattern matching is performed

Basic Example šŸ“

Let's see an example where we replace all occurrences of 'a' with 'e'.

cpp
#include <string> #include <regex> int main() { std::string my_string = "This is a test"; std::regex pattern(R"((a))"); // Notice the R before the string literal std::string replacement = "e"; std::regex_replace(my_string, pattern, replacement); std::cout << my_string << std::endl; return 0; }

Output:

This is etst

Capturing Groups šŸ’”

Capturing groups allow you to match specific parts of a pattern. Let's say we want to replace all occurrences of the word 'apple' with 'orange' and 'pear' with 'banana' in a sentence.

cpp
#include <string> #include <regex> int main() { std::string my_string = "I have apples and pears."; std::regex pattern(R"(( apple | pear ))"); // Notice the pipe (|) for OR operator std::string replacement = "orange"; // for apple std::string replacement2 = "banana"; // for pear std::regex_replace(my_string, pattern, [&](const std::match_results<std::string::const_iterator>& m) { if (m[1].matched) return replacement; // if it's apple return replacement2; // if it's pear }); std::cout << my_string << std::endl; return 0; }

Output:

I have oranges and bananas.

Summary āœ…

In this lesson, we explored C++'s std::regex_replace function, a powerful tool for string manipulation using regular expressions. We covered the basics of strings and regular expressions in C++, and saw examples of replacing single characters and complex patterns with capturing groups.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What does `std::regex_replace` function do in C++?

Quick Quiz
Question 1 of 1

In the given example, what does the `[&]` do in the lambda function passed to `std::regex_replace`?