How To Replace a Double Set of Quotation Marks with a Single set of Quotation Marks

Viewed 84

I am parsing some manifest files and need to sanitize them before I can load them as XML. So, these files are invalid XML files.

Consider the following snippet:

<assemblyIdentity name=""Microsoft.Windows.Shell.DevicePairingFolder"" processorArchitecture=""amd64"" version=""5.1.0.0"" type="win32" />

There are several instances of double quotes, "", that I want to replace with single occurrences, ".

Essentially, the example would be transformed to

<assemblyIdentity name="Microsoft.Windows.Shell.DevicePairingFolder" processorArchitecture="amd64" version="5.1.0.0" type="win32" />

I presume a regex would be the best approach here, however, it is not my strong point.

The following should be noted:

  • The manifest is a multiline string (essentially just an XML document)
  • Something like processorArchitecture="" is valid in the document hence why a simple string.Replace call is not appropriate.
3 Answers

Two ways:

  1. String replace
var newString = s.Replace("\"\"", "\"");
  1. Regex.
string checkStringForDoubleQuotes = @"""";
string newString  = Regex.Replace(s, checkStringForDoubleQuotes , @""");

After update:

Your regex is this https://regex101.com/r/xZUtUf/1/

""(?=\w)|(?<=\w)""
string s = "test=\"\" test2=\"\"assdasad\"\"";
string checkStringForDoubleQuotes = "\"\"(?=\\w)|(?<=\\w)\"\"";
string newString  = Regex.Replace(s, checkStringForDoubleQuotes , "\"");
Console.WriteLine(newString);
// test="" test2="assdasad"

https://dotnetfiddle.net/FmWXUa

Use

(\w+=)""(.*?)""(?=\s+\w+=|$)

Replace with $1"$2". See proof.

Explanation

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ""                       '""'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  ""                       '""'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

C# example:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(\w+=)""""(.*?)""""(?=\s+\w+=|$)";
        string substitution = @"$1""$2""";
        string input = @"<assemblyIdentity name=""""Microsoft.Windows.Shell.DevicePairingFolder"""" processorArchitecture=""""amd64"""" version=""""5.1.0.0"""" type=""win32"" />";
        
        Regex regex = new Regex(pattern);
        string result = regex.Replace(input, substitution);
        Console.Write(result);
    }
}

Use the hex escape for quotes as \x22 to make it easier to work with. This will replace each individual consecutive "" to ".

 Regex.Replace(data, @"(\x22\x22)", "\x22")
Related