C ++ regex: replace \ d \ s (with \ d * (

In the ss line, how do I replace "3 ("

with "3*("

? (It should work at all for any number.)

std::string result;
std::string ss;
static const std::regex nn1 ("\\)(\\d)");
static const std::regex nn2 ("(\\d)(\\s\\()");

ss = "5 + 3 (2 + 1)";
std::regex_replace (std::back_inserter(result), ss.begin(), ss.end(), nn2, "\d*($2");
std::cout << result << "\n";

      

Compiler error line 7 - '\d'

- unrecognized escape sequence. (I tried '\\d'

there.)

MS Visual Studio 2013

(Do not cheat on the proposed question, as this is about changing a character, not inserting it, and this is about a limitation that you cannot use by regular expressions in replacement chains and have to work on what is solved in the selected answer by using $ 1 first.)

+3


source to share


3 answers


As a more general way for such strings (but with one parenthesis), you can use the following regex:

(\d)\s?\(

      

And replace with:

$1\*\(

      



$1

will match the first group in your regex that is the digit before the parenthesis.

Edit: and in C ++, you can do:

std::regex_replace (std::back_inserter(result), ss.begin(), ss.end(), nn2, "$1\*\("); 

      

+1


source


The compiler error occurs because you are using an escape sequence \d

inside a C ++ string that simply does not exist. Worse, you are trying to use a regex pattern inside a replacement string, which you don't want. You want to use some literal string in there with backreferences like $1

( $

+ digit referring to the capturing group in a regex pattern

).

Use this code:

std::string result;
std::string ss;
static const std::regex nn1 ("\\)(\\d)");
static const std::regex nn2 ("(\\d+)\\s*\\(");

ss = "5 + 3 (2 + 1)";
std::regex_replace (std::back_inserter(result), ss.begin(), ss.end(), nn2, "$1*(");
std::cout << result << "\n";

      

Output:



enter image description here

nn2

the variable will contain a regex (\d+)\s*\(

that will capture

  • (\d+)

    - numbers, 1 or more, but as many as possible, and place in group 1
  • \s*\(

    - 0 or more spaces and literal parentheses

When replacing, we will refer to the first group as $1

. No need to run *

and (

.

+1


source


try {
    ResultString = TRegEx::Replace(SubjectString, "\"(\\d+) \\(\"", "\"$1*(\"", TRegExOptions() << roSingleLine << roMultiLine);
} catch (ERegularExpressionError *ex) {
    // Syntax error in the regular expression
}

      

Regex Explanation:

"(\d+) \("

Options:  Exact spacing; Dot matches line breaks; ^$ match at line breaks; Numbered capture

Match the character """ literally «"»
Match the regex below and capture its match into backreference number 1 «(\d+)»
   Match a single character that is a "digit" «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character " " literally « »
Match the character "(" literally «\(»
Match the character """ literally «"»

"$1*("

Insert the character """ literally «"»
Insert the text that was last matched by capturing group number 1 «$1»
Insert the character string "*("" literally «*("»

      

0


source







All Articles