How do regular expressions replace multiple matches?

奇 卢 181 Reputation points
2021-06-12T13:08:23.697+00:00

Sample string: "Print Computer information" (Except double quotes)

I want to add "a" after "print" and "operation" before "information".

So the result string is: "Print a Computer operation information"

My regular expression pattern is: @"(Print)(?:.+)(?=information)"

My replacement string is: “$1 a operation”.

But the result is not correct... How to replace it?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-06-12T16:15:25.127+00:00

    I think that you can use two replaces, or:

    string sample = "Print Computer information";
    string result = Regex.Replace( sample, @"(Print )(.*)( information)", "$1a $2 operation$3" );
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.