newstudent
newstudent

Reputation: 75

How can I replace only first found match of regex_search?

I am doing an exercise:

add code for the myPrintf template function that checks the matching of parameter types and type specifications in a format string. Limit only to %d as a conversion for int, then %f as a conversion for float and %s as a conversion for const char *.

Here is my code:

#include <iostream>
#include <string>
#include <type_traits>
#include <cstring>
#include <algorithm>
#include <regex>
using namespace std;

At the end I will have only this string to print with all %d%f%s changed to values of variables

auto myPrintf(const char* x){
    cout<< x;
}

Here is my template:

template<typename T ,typename... Args>
auto myPrintf(const char* x, T t, Args... args){
  std::regex e("[%].");
       std::cmatch cm;
      if (std::regex_search (x,cm,e)) 
    {           
    if (std::is_integral_v<T> ){       
     const char* d="%d";
     if (strcmp(cm,d)==0){
     std::regex_replace (x,e,t,std::regex_constants::format_first_only);
     }
    if(std::is_floating_point_v<T> ){ 
     const char* f="%f";
     if (strcmp(cm,f)==0){
     std::regex_replace (x,e,t,std::regex_constants::format_first_only);
     }        
    }
    if constexpr (std::is_same_v<const char*, T>) { 
             const char* s="%s";
     if (strcmp(cm,s)==0){
     std::regex_replace (x,e,t,std::regex_constants::format_first_only);
     }
    }

    } 
    return myPrintf(x,args...);


}

int main () {

    int i1 = 5, i2 = 7;
    float f1 = 5.4, f2 = 7.3;
    const char* s1 = "ABC";
    const char* s2 = "PQR";
    
    myPrintf("Print with no args");
    myPrintf("Print mix with err  -> str = %s, int = %d, flo = %f, str = %s, int = %d",
                                          s1, i1, f1, i1, i2);
    return 0;
}

Please give me the function I should use to replace the values in const char * string.

EDIT:

I have now this error: cannot convert ‘const value_type {aka const std::sub_match}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’

Thank you

Upvotes: 0

Views: 139

Answers (1)

Jarod42
Jarod42

Reputation: 217428

Fixed version of your code:

auto myPrintf(const std::string& x){
    std::cout<< x;
}

template<typename T ,typename... Args>
auto myPrintf(std::string x, T t, Args... args){
    std::regex e("[%].");
    std::smatch cm;
    if (std::regex_search (x,cm,e)) 
    {           
        if constexpr (std::is_integral_v<T> ){       
            const char* d="%d";
            if (strcmp(cm[0].str().c_str(),d)==0){
                x = std::regex_replace (x,e, std::to_string(t),std::regex_constants::format_first_only);
            }
        }
        if constexpr (std::is_floating_point_v<T> ){ 
            const char* f="%f";
            if (strcmp(cm[0].str().c_str(),f)==0){
                x = std::regex_replace (x,e,std::to_string(t),std::regex_constants::format_first_only);
            }        
        }
        if constexpr (std::is_same_v<const char*, T>) { 
            const char* s="%s";
            if (strcmp(cm[0].str().c_str(),s)==0){
                x = std::regex_replace (x,e,t,std::regex_constants::format_first_only);
            }
        }
    } 
    return myPrintf(x,args...);
}

Demo

Upvotes: 2

Related Questions