Jack
Jack

Reputation: 13

Regex to replace double nested quotes in C#

I am trying to replace double nested quotes from string in C# using Regex, but not able to achieve it so far. Below is the sample text and the code i tried -

string html = "<img src=\"imagename=\"g1\"\" alt = \"\">";
string output = string.Empty;
Regex reg = new Regex(@"([^\^,\r\n])""""+(?=[^$,\r\n])", RegexOptions.Multiline); 
output = reg.Replace(html, @"$1");

the above gives below output -

"<img src="imagename="g1 alt = >"

actual output i am looking for is -

"<img src="imagename=g1" alt = "">"

Please suggest how to correct the above code.

Upvotes: 0

Views: 647

Answers (1)

Braj
Braj

Reputation: 46841

Pattern : \s*"\s*([^ "]+)"\s*(?=[">])|(?<=")("")(?=")

Replacement : $1

Here is demo and tested at regexstorm

String literals for use in programs:

@"\s*""\s*([^ ""]+)""\s*(?=["">])|(?<="")("""")(?="")"

To keep it simple and more precised directly focused for src attribute value

Pattern : (\bsrc="[^ =]+=)"([^ "]+")"

Replacement : $1$2

Here is online demo and tested at regexstorm

String literals for use in programs:

@"(\bsrc=""[^ =]+=)""([^ ""]+"")"""

Note: I assume attribute values don't contain any spaces.

Upvotes: 2

Related Questions