Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:28:19+00:00 2026-05-24T11:28:19+00:00

I am having a regex replace issue that i can’t seem to figure out

  • 0

I am having a regex replace issue that i can’t seem to figure out for replacing some configured parameter for a file path.

Here is what I have so far:

The regex for a filepath may not be perfect but it seems to work ok.

regex: ^(?<path>[^\\/*?<>|]+)\\\\(?<filename>.+)\\.(?<ext>.mp4$)

file name match results name: $2

So what this is doing is searching a listing of files where the extension is mp4 and using the configured match result, it will return that as a “file name”.

Target string examples,

\\\\folder\music\hello.mp4

result filename = “hello”

What I would like to do is be able to take either the results from a regex match and be able to replace the name of the file/extension/path by a configured setting.

So If someone wanted for all the matched results to replace the file name with “goodbye”, how would i accomplish this. This is what i have now.

std::string sz_regex_pattern("^(?<path>[^\/*?<>|]+)\\(?<filename>.+)\.(?<ext>.mp4$)");
boost::cmatch rm;
boost::regex pattern(sz_regex_pattern, regex::icase|regex_constants::perl);
std::string complete_file_name_path = "\\folder\music\hello.mp4";
bool result = boost::regex_match(complete_file_name_path , rm, pattern);
std::string old_filename= rm.format("$2"); // returns the name of the file only

What appears to work but limits it to a filename where the folder is not the same name so,
\\folder\music\hello\hello.mp4 would have issues with the regex_replace below.

std::string new_filename = "goodbye";

std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);

so i can later,

boost::filesystem::rename(complete_file_name_path, sz_new_file_name_path);

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:28:20+00:00Added an answer on May 24, 2026 at 11:28 am

    Find and replace is completely unnecessary because you already have all of the components you need to build the new path.

    REPLACE

    std::string sz_new_file_name_path = boost::regex_replace(complete_file_name_path, old_filename, new_filename);
    

    WITH

    // path + newFileName + ext
    std::string sz_new_file_name_path = rm.format("$1") + "\\" + new_filename + "." + rm.format("$3")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with my regex. I want to capture <% some stuff
Currently I have some code to replace strings in a file that looks like
I am having problems with RegEx in PHP and can't seem to find the
I'm having a problem finding out how to replace the last ', ' in
I have some JavaScript that runs uses a replace with regular expressions to modify
Having regex issues.. I have a series of directories that hold .htm templates... all
I'm doing a Replace operation on a large file. I'm having a problem with
For clarification, here's the exact question in the about_regular_expressions.rb file that I'm having trouble
I am having some troubles getting some regex to work to remove some text,
I'm having trouble coming up with the correct regex string to remove a sequence

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.