question

58901228 avatar image
0 Votes"
58901228 asked Viorel-1 answered

How do regular expressions replace multiple matches?

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?



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

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" );


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.