Search and Replace
Transform text with regex.
Search and Replace is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
regex_replace
std::regex_replace returns a new string where every match of a pattern is replaced by a replacement string.
- The original string is left unchanged.
- By default, all matches are replaced.
#include <iostream>
#include <regex>
int main() {
std::regex p("cat");
std::string s = "the cat and the cat";
std::cout << std::regex_replace(s, p, "dog") << '\n';
return 0;
}Replacing Digits
Patterns make bulk edits easy, such as masking every digit in a string with a placeholder.
#include <iostream>
#include <regex>
int main() {
std::regex p("\\d");
std::string s = "PIN 1234";
std::cout << std::regex_replace(s, p, "*") << '\n';
return 0;
}Backreferences in Replacement
Use $1, $2, ... in the replacement string to insert captured groups, letting you rearrange text.
#include <iostream>
#include <regex>
int main() {
std::regex p("(\\w+) (\\w+)");
std::string s = "John Smith";
std::cout << std::regex_replace(s, p, "$2 $1") << '\n';
return 0;
}The Whole Match: $&
The token $& refers to the entire match, handy for wrapping found text in extra characters.
#include <iostream>
#include <regex>
int main() {
std::regex p("\\d+");
std::string s = "item 42";
std::cout << std::regex_replace(s, p, "[$&]") << '\n';
return 0;
}Replace First Only
Pass std::regex_constants::format_first_only to replace only the first match instead of every occurrence.
#include <iostream>
#include <regex>
int main() {
std::regex p("o");
std::string s = "foo boo";
auto out = std::regex_replace(s, p, "0", std::regex_constants::format_first_only);
std::cout << out << '\n';
return 0;
}Normalizing Whitespace
A common cleanup task: collapse runs of whitespace into a single space.
#include <iostream>
#include <regex>
int main() {
std::regex p("\\s+");
std::string s = "too many spaces";
std::cout << std::regex_replace(s, p, " ") << '\n';
return 0;
}Removing Matches
Replacing with an empty string effectively deletes every match, useful for stripping unwanted characters.
#include <iostream>
#include <regex>
int main() {
std::regex p("[^a-zA-Z]");
std::string s = "a1b2c3";
std::cout << std::regex_replace(s, p, "") << '\n';
return 0;
}Reformatting Dates
Capture parts of a date and reorder them in the replacement to change the format.
#include <iostream>
#include <regex>
int main() {
std::regex p("(\\d{4})-(\\d{2})-(\\d{2})");
std::string s = "2026-05-30";
std::cout << std::regex_replace(s, p, "$3/$2/$1") << '\n';
return 0;
}Keeping Unmatched Text
By default unmatched text is copied through. Use format_no_copy to output only the replacements and drop the rest.
#include <iostream>
#include <regex>
int main() {
std::regex p("\\d+");
std::string s = "a1b2c3";
auto out = std::regex_replace(s, p, "$&", std::regex_constants::format_no_copy);
std::cout << out << '\n';
return 0;
}Censoring Words
Combine alternation with replacement to censor a list of words at once.
#include <iostream>
#include <regex>
int main() {
std::regex p("bad|evil");
std::string s = "a bad and evil idea";
std::cout << std::regex_replace(s, p, "***") << '\n';
return 0;
}Building Up a Result
Because regex_replace returns a string, you can chain transformations by feeding one result into the next call.
#include <iostream>
#include <regex>
int main() {
std::string s = " Hello, World ";
s = std::regex_replace(s, std::regex("\\s+"), " ");
s = std::regex_replace(s, std::regex("^ | $"), "");
std::cout << "[" << s << "]\n";
return 0;
}Quick Check
Test your understanding of replacement syntax.
Recap
You learned to transform text with regex_replace:
- replaces all matches by default and returns a new string
$1,$2,$&insert captures and the whole match- flags like
format_first_onlyandformat_no_copy - chain calls to build complex cleanups
Next, you will learn how to use regex efficiently and avoid common performance traps.
Frequently asked questions
Is the “Search and Replace” lesson free?
Yes — the full text of “Search and Replace” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Search and Replace”?
Transform text with regex. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Search and Replace” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- regex Basics
- Capturing Groups
- Search and Replace
- Performance Notes