How to replace the string enclose in double quote using regex?

I have used regex to find and replace the string enclose within double quotes. Below is the regex I have used. Output I got is new is placed before the regex.

int portNum = 5969;
var input = File.ReadAllText(@"C:\Users\aaa\look.ts");
var outputFileText = Regex.Replace(input, @"baseurl(\s*)= (\s*).*?", @"baseurl = " + "\"http://localhost:" + portNum + "\"");

Output I got is

var baseurl = "http://localhost:5969/""http://www.google.com/";

Expected output is

var baseurl= "http://localhost:5969/"

Upvotes: 1

Views: 152

Answers (1)

anubhava
anubhava

Reputation: 786289

You should not use greedy quantifier .*? in the end otherwise it will match as little as possible. In this case, since there is nothing after .*? therefore it will match 0 characters.

To make it work, make it greedy to match till end:

int portNum = 5969;
var input = File.ReadAllText(@"C:\Users\aaa\look.ts");
var outputFileText = Regex.Replace(input, @"baseurl(\s*)= (\s*).*",
                     @"baseurl = " + "\"http://localhost:" + portNum + "\"");

Upvotes: 1

Related Questions