0

I would like to find $number substrings and then replace them with $number + 1 format.

For example, $1 should become $2 in a string.

So far, I found out how to find $number pattern in a string and then replace them with other string using regex and it works fine.

My Code :

#include <iostream>
#include <string>
#include <regex>

std::string replaceDollarNumber(std::string str, std::string replace)
{
    std::regex long_word_regex("(\\$[0-9]+)");
    std::string new_s = std::regex_replace(str, long_word_regex, replace);
    return new_s;
}

int main()
{
    std::string str = "!$@#$34$1%^&$5*$1$!%$91$12@$3";
    auto new_s = replaceDollarNumber(str, "<>");
    std::cout << "Result : " << new_s << '\n';
}

The Result :

Result : !$@#<><>%^&<>*<>$!%<><>@<>

The Result I want :

Result : !$@#$35$2%^&$6*$2$!%$92$13@$4

Is it possible to do this using regex?

Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • It seems you may port [this solution](https://stackoverflow.com/a/12946132/3832970) to C++. – Wiktor Stribiżew Aug 02 '18 at 12:19
  • 6
    You could probably do it with capture groups and then parsing each capture to get the number and do something with that. Or use simpler techniques than regular expressions and parse the input manually. Remember the saying: "I have one problem. I solved it with a regex. Now I have *two* problems". For most cases regular expressions is like killing a mosquito by shotgun. It might work, but there's a lot of collateral damage. – Some programmer dude Aug 02 '18 at 12:19
  • Possible duplicate of [regex replace with callback in c++11?](https://stackoverflow.com/questions/22617209/regex-replace-with-callback-in-c11) – Sebastian Proske Aug 02 '18 at 12:19
  • @Someprogrammerdude So you recommend not using regex and do it manually instead? I will follow your advice then. – Zack Lee Aug 02 '18 at 12:35
  • 2
    I would definitely start with a non-regex solution or two. – Some programmer dude Aug 02 '18 at 12:58
  • use this https://msdn.microsoft.com/en-us/library/cft8645c.aspx – Veltzer Doron Aug 02 '18 at 13:02
  • @Someprogrammerdude Do you mind showing me a non-regex solution? – Zack Lee Aug 02 '18 at 13:02
  • 3
    Start by looping over the input string, character by character. Add copying the characters (one by one) to a new string. Add recognition of the dollar character `'$'`. Add checking of digits following the `'$'`. Add getting all digits after the `'$'` (without copying them). Add converting the digit characters to an integer value. Add adding `1` to that value. Add inserting the new value into the destination as a string. Done. :) – Some programmer dude Aug 02 '18 at 13:11

1 Answers1

1

Consider the following approach

#include <iostream>
#include <string>
#include <vector>
#include <regex>
using std::string;
using std::regex;
using std::sregex_token_iterator;
using std::cout;
using std::endl;
using std::vector;


int main()
{
    regex re("(\\$[0-9]+)");
    string s = "!$@#$34$1%^&$5*$1$!%$91$12@$3";
    sregex_token_iterator it1(s.begin(), s.end(), re);
    sregex_token_iterator it2(s.begin(), s.end(), re, -1);
    sregex_token_iterator reg_end;
    vector<string> vec;
    string new_str;
    cout << s << endl;
    for (; it1 != reg_end; ++it1){ 
        string temp;
        temp = "$" + std::to_string(std::stoi(it1->str().substr(1)) + 1);
        vec.push_back(temp);
    }
    int i(0);
    for (; it2 != reg_end; ++it2) 
        new_str += it2->str() + vec[i++];

    cout << new_str << endl;

}

The result is

!$@#$34$1%^&$5*$1$!%$91$12@$3
!$@#$35$2%^&$6*$2$!%$92$13@$4
CroCo
  • 5,531
  • 9
  • 56
  • 88