Question

I have description field which is:

16" Alloy Upgrade 

In CSV format it appears like this:

"16"" Alloy Upgrade "

What would be the best use of regex to maintain the original format? As I'm learning I would appreciate it being broke down for my understanding.

I'm already using Regex to split some text separating 2 fields which are: code, description. I'm using this:

 ,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))

My thoughts are to remove the quotes, then remove the delimiter excluding use in sentences.

Thanks in advance.

Was it helpful?

Solution

If you don't want to/can't use a standard CSV parser (which I'd recommend), you can strip all non-doubled quotes using a regex like this:

Regex.Replace(text, @"(?!="")""(?!"")",string.Empty)

That regex will match every " character not preceded or followed by another ".

OTHER TIPS

I wouldn't use regex since they are usually confusing and totally unclear what they do (like the one in your question for example). Instead this method should do the trick:

public string CleanField(string input)
{
    if (input.StartsWith("\"") && input.EndsWith("\""))
    {
        string output = input.Substring(1,input.Length-2);
        output = output.Replace("\"\"","\"");
        return output;
    }
    else
    {
        //If it doesn't start and end with quotes then it doesn't look like its been escaped so just hand it back
        return input;
    }
}

It may need tweaking but in essence it checks if the string starts and ends with a quote (which it should if it is an escaped field) and then if so takes the inside part (with the substring) and then replaces double quotes with single quotes. The code is a bit ugly due to all the escaping but there is no avoiding that.

The nice thing is this can be used easily with a bit of Linq to take an existing array and convert it.

processedFieldArray = inputfieldArray.Select(CleanField).ToArray();

I'm using arrays here purely because your linked page seems to use them where you are wanting this solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top