1

I'm trying to replace all instances of the key INPUT_CSV in a string with a file path that is guaranteed to not have any single or double quotes (or other unsafe characters for paths) within it. However, this file path will be surrounded by double quotes.

The key may be surrounded by double quotes, single quotes, or no quotes at all in the input string. I want to be as flexible as possible, and replace all of those instances with the same, double-quoted file path.

This is how I'm currently doing it:

Dim doubleQuotedCsvFilePath As String = """" & generatedCsvFilePath & """"

scriptStr = scriptStr.Replace("""INPUT_CSV""", doubleQuotedCsvFilePath)
scriptStr = scriptStr.Replace("'INPUT_CSV'", doubleQuotedCsvFilePath)
scriptStr = scriptStr.Replace("INPUT_CSV", doubleQuotedCsvFilePath)

Is there some sort of nifty regex (or possibly some other method) that can do essentially this more succinctly? If this is the best way to do it, I'm fine with that too - just wondering if there is a better way. Thanks!

P.S. Not a requirement, but if there was some way to make the regex case-insenstive too (so that e.g. "input_csv", 'input_CSV', or INPut_CSV all get replaced), that would be great also. Thanks!

benjammin
  • 537
  • 5
  • 23
  • Are you using a [csv parser](http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-sharp) or rolling your own? Consider the former, you'll save yourself a LOT of headache down the road. – crthompson Apr 16 '15 at 20:25
  • As for the string filtering, you can add .ToUpper() to the string replacement then you know case won't be an issue, that is assuming you don't want the replacement to be case sensitive. – CalebB Apr 16 '15 at 20:26
  • `scriptStr = New Regex("(?i)(['""])input_csv(['""])").Replace(doubleQuotedCsvFilePath, "$1INPUT_CSV$2")` will do? – Wiktor Stribiżew Apr 16 '15 at 20:27
  • @paqogomez Nope, I'm not going to be parsing the contents of the CSV at all. – benjammin Apr 16 '15 at 20:29

2 Answers2

2

You can use back reference for matching the first quote:

  string scriptStr= "'INPUT_CSV'";
  string pattern = @"(""|'?)INPUT_CSV\1";
  Regex rgx = new Regex(pattern);
  scriptStr = rgx.Replace(scriptStr, doubleQuotedCsvFilePath);

See https://msdn.microsoft.com/en-us/library/thwdfzxy%28v=vs.110%29.aspx

dpolivaev
  • 494
  • 3
  • 12
2

Here is my attempt:

(?i)(['""]?)input_csv\1

(?i) turns on case-insensitive search, then we capture an optional single or double quote into a capturing group, match the input_csv string and then check if there is the same closing quote as the opening one.

VB.NET:

Dim doubleQuotedCsvFilePath As String = """" & "InPuT_CsV" & """"
Dim scriptStr As String
Dim m As Regex = New Regex("(?i)(['""]?)input_csv\1")
scriptStr = m.Replace(doubleQuotedCsvFilePath, "$1INPUT_CSV$1")

C#:

var doubleQuotedCsvFilePath = "\"InPuT_CsV\"";
var scriptStr = string.Empty;
var m = new Regex(@"(?i)(['""]?)input_csv\1");
scriptStr = m.Replace(doubleQuotedCsvFilePath, "$1INPUT_CSV$1");

Output:

"INPUT_CSV"

DISCLAIMER: Only use it if you are not re-inventing the wheel with a CSV parser.

benjammin
  • 537
  • 5
  • 23
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This works great for when the string is surrounded by single or double quotes, but it doesn't work for the no-quotes scenario. Seems like that's probably just a minor enhancement to the regex though, right? Also, I think the arguments to Replace are reversed (just an fyi). – benjammin Apr 16 '15 at 20:43
  • I made the quote optional by adding the `?` next to `['""]`. Also, this answer shows how to enforce case insensitive search and replace with inline `(?i)` option. I added C# code. – Wiktor Stribiżew Apr 16 '15 at 20:52
  • Okay, cool. I went ahead and accepted your answer since you also address the case-insensitivity issue. I didn't need the C# code, but maybe that will be useful to someone else. Thanks! – benjammin Apr 16 '15 at 20:59