Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My answer was similar to Nas Banov's answer but I wanted <em>double quotes</em> only if necessary.</p> <h1>Cutting out extra unnecessary double quotes</h1> <p>My code saves unnecessarily putting <em>double quotes</em> around it all the time which is important *when you are getting up close to the character limit for parameters.</p> <pre><code>/// &lt;summary&gt; /// Encodes an argument for passing into a program /// &lt;/summary&gt; /// &lt;param name="original"&gt;The value that should be received by the program&lt;/param&gt; /// &lt;returns&gt;The value which needs to be passed to the program for the original value /// to come through&lt;/returns&gt; public static string EncodeParameterArgument(string original) { if( string.IsNullOrEmpty(original)) return original; string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0"); value = Regex.Replace(value, @"^(.*\s.*?)(\\*)$", "\"$1$2$2\""); return value; } // This is an EDIT // Note that this version does the same but handles new lines in the arugments public static string EncodeParameterArgumentMultiLine(string original) { if (string.IsNullOrEmpty(original)) return original; string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0"); value = Regex.Replace(value, @"^(.*\s.*?)(\\*)$", "\"$1$2$2\"", RegexOptions.Singleline); return value; } </code></pre> <h2>explanation</h2> <p>To escape the <em>backslashes</em> and <em>double quotes</em> correctly you can just replace any instances of multiple <em>backslashes</em> followed by a single <em>double quote</em> with:</p> <pre><code>string value = Regex.Replace(original, @"(\\*)" + "\"", @"\$1$0"); </code></pre> <p>An extra twice the original <em>backslashes</em> + 1 and the original <em>double quote</em>. i.e., '\' + originalbackslashes + originalbackslashes + '"'. I used $1$0 since $0 has the original <em>backslashes</em> and the original <em>double quote</em> so it makes the replacement a nicer one to read.</p> <pre><code>value = Regex.Replace(value, @"^(.*\s.*?)(\\*)$", "\"$1$2$2\""); </code></pre> <p>This can only ever match an entire line that contains a whitespace.</p> <p>If it matches then it adds <em>double quotes</em> to the beginning and end.</p> <p>If there was originally <em>backslashes</em> on the end of the argument they will not have been quoted, now that there is a <em>double quote</em> on the end they need to be. So they are duplicated, which quotes them all, and prevents unintentionally quoting the final <em>double quote</em></p> <p>It does a minimal matching for the first section so that the last .*? doesn't eat into matching the final <em>backslashes</em></p> <h1>Output</h1> <p>So these inputs produce the following outputs</p> <p>hello</p> <p><strong>hello</strong></p> <p>\hello\12\3\</p> <p><b>\hello\12\3\</b></p> <p>hello world</p> <p>"hello world"</p> <p>\"hello\"</p> <p><strong>\\"hello\\\"</strong></p> <p>\"hello\ world</p> <p><strong>"\\"hello\ world"</strong></p> <p>\"hello\\\ world\</p> <p><strong>"\\"hello\\\ world\\"</strong></p> <p>hello world\\</p> <p><strong>"hello world\\\\"</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload