Regular Expressions

Regex Replace in File

Can regex be used for replacing text in a file? How do I implement this in C++?

Abstract art representing computer programming

Yes, regex can be used for replacing text in a file in C++. Here's a step-by-step guide on how to implement this.

Step 1: Read the File Content

First, you need to read the content of the file into a std::string. You can use std::ifstream to do this. For example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>

std::string readFile(const std::string& filePath) {
  std::ifstream file{filePath};
  std::stringstream buffer;
  buffer << file.rdbuf();
  return buffer.str();
}

int main() {
  std::string filePath{"example.txt"};
  std::string content = readFile(filePath);

  std::cout << "Original Content:\n" << content;
}

Step 2: Perform Regex Replacement

Next, use std::regex_replace to replace text in the file content. This function takes the input string, a regex pattern, and the replacement string. For example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>

std::string readFile(const std::string& filePath) {
  std::ifstream file{filePath};
  std::stringstream buffer;
  buffer << file.rdbuf();
  return buffer.str();
}

int main() {
  std::string filePath{"example.txt"};
  std::string content = readFile(filePath);

  // Replace the word 'word'
  std::regex pattern{R"(word)"};
  std::string replacement{"replacement"};
  std::string newContent = std::regex_replace(
    content, pattern, replacement
  );

  std::cout << "Modified Content:\n"
    << newContent << "\n";
}

Step 3: Write the Modified Content Back to the File

Finally, write the modified content back to the file using std::ofstream. For example:

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>

std::string readFile(const std::string& filePath) {
  std::ifstream file{filePath};
  std::stringstream buffer;
  buffer << file.rdbuf();
  return buffer.str();
}

void writeFile(const std::string& filePath,
               const std::string& content) {
  std::ofstream file{filePath};
  file << content;
}

int main() {
  std::string filePath{"example.txt"};
  std::string content = readFile(filePath);

  std::regex pattern{R"(\bword\b)"};
  std::string replacement{"replacement"};
  std::string newContent = std::regex_replace(
    content, pattern, replacement
  );

  writeFile(filePath, newContent);

  std::cout << "File updated successfully.";
}

Full Example

Combining all steps, here is the full implementation:

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>

std::string readFile(const std::string& filePath) {
  std::ifstream file{filePath};
  std::stringstream buffer;
  buffer << file.rdbuf();
  return buffer.str();
}

void writeFile(const std::string& filePath,
               const std::string& content) {
  std::ofstream file{filePath};
  file << content;
}

int main() {
  std::string filePath{"example.txt"};
  std::string content = readFile(filePath);

  std::regex pattern{R"(word)"};
  std::string replacement{"replacement"};
  std::string newContent = std::regex_replace(
    content, pattern, replacement
  );

  writeFile(filePath, newContent);

  std::cout << "File updated successfully.\n";
}

This program reads the content of "example.txt", replaces all occurrences of the word "word" with "replacement", and writes the modified content back to the file.

Adjust the regex pattern and replacement string as needed for your specific use case.

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