Repository: microsoft/PowerToys
File: src/modules/AdvancedPaste/AdvancedPaste/Helpers/JsonHelper.cs (C#)

Code before:
            }

            // Try convert CSV
            try
            {
                if (string.IsNullOrEmpty(jsonText))
                {
                    var csv = new List<string[]>();

                    string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    // Detect the csv delimiter and the count of occurrence based on the first two csv lines.
                    GetCsvDelimiter(lines, out char delim, out int delimCount);

                    foreach (var line in lines)
                    {
                        // If line is separator property line, then skip it
                        if (CsvSepIdentifierRegex.IsMatch(line))
                        {
                            continue;
                        }

                        // A CSV line is valid, if the delimiter occurs more or equal times in every line compared to the first data line. (More because sometimes the delimiter occurs in a data string.)
                        if (line.Count(x => x == delim) >= delimCount)
                        {
                            csv.Add(Regex.Split(line, delim + @"(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"));
                        }
                        else
                        {
                            throw new FormatException("Invalid CSV format: Number of delimiters wrong in the current line.");
                        }
                    }

                    Logger.LogDebug("Convert from csv.");
                    jsonText = JsonConvert.SerializeObject(csv, Newtonsoft.Json.Formatting.Indented);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed parsing input as csv", ex);
            }

            // Try convert Plain Text
            try
            {
                if (string.IsNullOrEmpty(jsonText))
                {
                    var plainText = new List<string>();

                    foreach (var line in text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                    {

Review comment (security):
It makes sense to move the regex as static readonly variable to the top. Line 23 to 25. 

And we should add a comment to describe how the regex works. Something like `"split on every occurence of the delim except if it is enclosed by " and ignore two " as escaped ".`".

Code after:
            catch (Exception ex)
            {
                Logger.LogError("Failed parsing input as ini", ex);
            }

            // Try convert CSV
            try
            {
                if (string.IsNullOrEmpty(jsonText))
                {
                    var csv = new List<string[]>();

                    string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                    // Detect the csv delimiter and the count of occurrence based on the first two csv lines.
                    GetCsvDelimiter(lines, out char delim, out int delimCount);

                    foreach (var line in lines)
                    {
                        // If line is separator property line, then skip it
                        if (CsvSepIdentifierRegex.IsMatch(line))
                        {
                            continue;
                        }

                        // A CSV line is valid, if the delimiter occurs equal times in every line compared to the first data line
                        // and if every line contains no or an even count of quotation marks.
                        if (Regex.Count(line, delim + CsvDelimSepRegexStr) == delimCount && int.IsEvenInteger(line.Count(x => x == '"')))
                        {
                            csv.Add(Regex.Split(line, delim + CsvDelimSepRegexStr, RegexOptions.IgnoreCase));
                        }
                        else
                        {
                            throw new FormatException("Invalid CSV format: Number of delimiters wrong in the current line.");
                        }
                    }

                    Logger.LogDebug("Convert from csv.");
                    jsonText = JsonConvert.SerializeObject(csv, Newtonsoft.Json.Formatting.Indented);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Failed parsing input as csv", ex);
            }

            // Try convert Plain Text
            try
            {
                if (string.IsNullOrEmpty(jsonText))
                {