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

Code before:
                if (matchChar.Success)
                {
                    // We can do matchChar[0] as the match only returns one character.
                    // We get the count from the second line, as the first one only contains the character definition and not a CSV data line.
                    char delimChar = matchChar.Groups[1].Value.Trim()[0];
                    delimiter = delimChar;
                    delimiterCount = Regex.Count(csvLines[1], delimChar + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                }
            }

            if (csvLines.Length > 0 && delimiterCount == 0)
            {
                // Try to select the correct delimiter based on the first two CSV lines from a list of predefined delimiters.
                foreach (char c in CsvDelimArry)
                {
                    int cntFirstLine = Regex.Count(csvLines[0], c + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                    int cntNextLine = 0; // Default to 0 that the 'second line' check is always true.

                    // Additional count if we have more than one line
                    if (csvLines.Length >= 2)
                    {
                        cntNextLine = Regex.Count(csvLines[1], c + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                    }

                    // The delimiter is found if the count is bigger as from the last selected delimiter
                    // and if the next csv line does not exist or has the same number or more occurrences of the delimiter.
                    // (We check the next line to prevent false positives.)
                    if (cntFirstLine > delimiterCount && (cntNextLine == 0 || cntNextLine == cntFirstLine))
                    {
                        delimiter = c;
                        delimiterCount = cntFirstLine;
                    }
                }
            }

            // If the delimiter count is 0, we can't detect it and it is no valid CSV.
            if (delimiterCount == 0)
            {
                throw new FormatException("Invalid CSV format: Failed to detect the delimiter.");
            }
        }
    }
}


Review comment (suggestion):
```suggestion
                    // and if the next csv line does not exist or has the same number of occurrences of the delimiter.
```


Code after:
                Match matchChar = CsvSepIdentifierRegex.Match(csvLines[0]);
                if (matchChar.Success)
                {
                    // We can do matchChar[0] as the match only returns one character.
                    // We get the count from the second line, as the first one only contains the character definition and not a CSV data line.
                    char delimChar = matchChar.Groups[1].Value.Trim()[0];
                    delimiter = delimChar;
                    delimiterCount = Regex.Count(csvLines[1], delimChar + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                }
            }

            if (csvLines.Length > 0 && delimiterCount == 0)
            {
                // Try to select the correct delimiter based on the first two CSV lines from a list of predefined delimiters.
                foreach (char c in CsvDelimArry)
                {
                    int cntFirstLine = Regex.Count(csvLines[0], c + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                    int cntNextLine = 0; // Default to 0 that the 'second line' check is always true.

                    // Additional count if we have more than one line
                    if (csvLines.Length >= 2)
                    {
                        cntNextLine = Regex.Count(csvLines[1], c + CsvDelimSepRegexStr, RegexOptions.IgnoreCase);
                    }

                    // The delimiter is found if the count is bigger as from the last selected delimiter
                    // and if the next csv line does not exist or has the same number of occurrences of the delimiter.
                    // (We check the next line to prevent false positives.)
                    if (cntFirstLine > delimiterCount && (cntNextLine == 0 || cntNextLine == cntFirstLine))
                    {
                        delimiter = c;
                        delimiterCount = cntFirstLine;
                    }
                }
            }

            // If the delimiter count is 0, we can't detect it and it is no valid CSV.
            if (delimiterCount == 0)
            {
                throw new FormatException("Invalid CSV format: Failed to detect the delimiter.");
            }
        }
    }
}