1

I have the following RegEx.Replace:

Regex.Replace(line[k], "(?i)(?<!^)(?<!;)\"(?![\";])", "\"\"", RegexOptions.None);

line[k] = a string from a string[]

What i want it to do is replace all quotation marks (") with double quotation marks ("") but only if the quotation mark is NOT the beginning of the string and not preceded or followed by a ; as example:

"This is a "sentence" that starts with a quotation mark";"This is another sentence with a "quotation" mark preceded by a ; "

should be:

"This is a ""sentence"" that starts with a quotation mark";"This is another sentence with a ""quotation"" mark preceded by a ; "

I pretty much copied the regular expression from a regex builder and it worked there after i worked on it, but now in my application it doesnt work anymore. I even tested the same string in the regex builder and my own application.

Moonpaw
  • 55
  • 8
  • Do you get some error? or What is the result that you are getting? – Roman Dec 17 '15 at 15:18
  • 3
    Your [regex works](http://regexstorm.net/tester?p=(%3f%3c!%5e)(%3f%3c!%3b)%22(%3f!%5b%22%3b%5d)&i=%22This+is+a+%22sentence%22+that+starts+with+a+quotation+mark%22%3b%22This+is+another+sentence+with+a+%22quotation%22+mark+preceded+by+a+%3b+%22&r=%22%22). Do you assign the modified value to a variable? – Wiktor Stribiżew Dec 17 '15 at 15:19
  • 2
    Please define "doesn't work". What output are you getting instead? – juharr Dec 17 '15 at 15:19
  • I pretty much get the same string back. the rest of my code looks like this `Regex.Replace(line[k + 1], "(?i)(? – Moonpaw Dec 17 '15 at 15:21
  • Adding quotation marks at the beginning and end of a string is important because sometimes you just have string like: word1;word2. I want some degree of consistency at least Id also like to mention i cant format those damn comments for the life of me – Moonpaw Dec 17 '15 at 15:22
  • Remove the space between the ` and R. Or edit your question :) – Thomas Ayoub Dec 17 '15 at 15:24
  • @Thomas Well, i tried....Its at least a tiny bit better now – Moonpaw Dec 17 '15 at 15:26
  • 1
    never mind, im a dumb idiot. I *only* called Regex.Replace(string, pattern, Matchoptions); but i never assigned the return value to anything T.T I feel like a dumbass now, but at least i figured it out :/ Sorry for the perhaps unnecassary question – Moonpaw Dec 17 '15 at 15:50

1 Answers1

1

As far as I can tell, it works as expected. Here is a little LINQPad script, based on your example:

var orignial = "This is a \"sentence\" that starts with a quotation mark\";\"This is another sentence with a \"quotation\" mark preceded by a ;";
var result = Regex.Replace(orignial, "(?i)(?<!^)(?<!;)\"(?![\";])", "\"\"", RegexOptions.None);
Console.WriteLine(orignial);
Console.WriteLine(result);

The result I get from the 2 Runs: Console.WriteLine(orignial);

This is a "sentence" that starts with a quotation mark";"This is another sentence with a "quotation" mark preceded by a ;

Console.WriteLine(result);

This is a ""sentence"" that starts with a quotation mark";"This is another sentence with a ""quotation"" mark preceded by a ;

If you formatted the original string correctly, it should work as expected.

Roman
  • 1,399
  • 20
  • 26