Dmitriy Sosunov
Dmitriy Sosunov

Reputation: 1113

Need regular expression to close or replace quotes

Please help me solve my problem. I have a list of following strings:

a
"b
c"
"ddd"
'ee'
a"b"c
a'b'c

as a result I need:

"a"
"b"
"c"
"ddd"
"ee"
"a\"b\"c"
"a'b'c"

Please help me write pattern for replace.

Upvotes: 1

Views: 395

Answers (3)

Joey
Joey

Reputation: 354436

As others have already noted in comments, too: This isn't a job for regular expressions. In fact, if you try to do so you'll get code that is less readable and maintanable than the straightforward idea.

string.Concat("\"", myString.Trim('"', '\'').Replace("\"","\\\""), "\"");

PowerShell test:

PS Home:\> filter fixString { '"' + $_.Trim('"', "'").Replace('"', '\"') + '"' }
PS Home:\> 'a','"b','c"','"ddd"',"'ee'",'a"b"c',"a'b'c" | fixString
"a"
"b"
"c"
"ddd"
"ee"
"a\"b\"c"
"a'b'c"

However, if you desperately need regular expressions, I can think of only the following three individual replacements:

myString = Regex.Replace(myString, @"^['""]|['""]$", "");
myString = Regex.Replace(myString, @"""", @"\""");
myString = Regex.Replace(myString, "^|$", @"""");

PowerShell test:

PS Home:\> 'a','"b','c"','"ddd"',"'ee'",'a"b"c',"a'b'c" |
>> % { $_ -replace '^[''"]|[''"]$' -replace '"','\"' -replace '^|$','"' }
"a"
"b"
"c"
"ddd"
"ee"
"a\"b\"c"
"a'b'c"

The following works with only two regular expressions, thanks to Toader Mihai Claudiu who offered some crucial advice:

myString = Regex.Replace(@"^['""]?(.*?)['""]?$", @"""$1""");
myString = Regex.Replace(@"(?<=.)""(?=.)", @"\""");

PowerShell test:

PS Home:\> 'a','"b','c"','"ddd"',"'ee'",'a"b"c',"a'b'c" |
>> % { $_ -replace '^[''"]?(.*?)[''"]?$','"$1"' -replace '(?<=.)"(?=.)','\"' }
"a"
"b"
"c"
"ddd"
"ee"
"a\"b\"c"
"a'b'c"

Now ask yourself: Would you still know what the code does in a month from now? A year maybe?

Upvotes: 3

Mihai Toader
Mihai Toader

Reputation: 12243

You should do it manually. Add a " in a buffer, skip the first char in the string if " or ' add each non " as it is to the buffer and quote every " (aka convert it to \"). And at the end (don't add quoted " until you have a next char that is different than ") just add a non quoted " if the last char is " or '.

You can do this with maybe 2 regex search and replace but it's more efficient to do it as above.

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116100

Don't know the C# syntax, but I think you should split it in three easy steps.

For each string: - Strip start and end quote if any. - Escape the string. - Add start and end quote.

Should be easy to implement.

Upvotes: 1

Related Questions