Regex string replace

Posted on 2007-07-29
Last Modified: 2011-09-20
Hi Experts,
I would like to have a function like this in Visual C++ 6.0 in MFC

I downloaded the boost Regex++ library but it doesn't look like they have an API for replace... Why wouldn't they? (is it because the replace is very often ambiguous)?

Thanks for your help with this!
Question by:thready
    LVL 19

    Expert Comment

    LVL 1

    Author Comment

    Yes - but how can I use this from Visual C++ 6.0?
    LVL 19

    Accepted Solution

    Build the regex library according to the directions at "<boost>/libs/regex/index.html".  See build instructions for VC6.

    Then write a program like:

    // VC6Console.cpp : Defines the entry point for the console application.

    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <boost/regex.hpp>

    int main(int argc, char* argv[])
          printf("Hello World!\n");
          std::string s1 = "This is string number one.";
          std::string s2 = "NUMBER";
          boost::regex rx("number");
          s1 = boost::regex_replace(s1, rx, s2);
          std::cout << s1.c_str();
          return 0;

    which will output "This string is NUMBER one."

    There is also another form of regex_replace that is fancier.
    LVL 19

    Expert Comment

    It's not too hard.  I downloaded/built/wrote/ran in about 15 minutes.
    LVL 1

    Author Comment

    Many thanks drichards!  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now