Home > Backend Development > C++ > How to Split a String in C# Using Regex to Handle Commas Within Quotes?

How to Split a String in C# Using Regex to Handle Commas Within Quotes?

Mary-Kate Olsen
Release: 2025-01-07 22:11:41
Original
611 people have browsed it

"How

Using regular expressions to split strings in C#: processing commas within quotes

In various scenarios such as data parsing or SQL string operations, strings containing commas may need to be split using regular expressions. However, challenges arise when commas appear inside and outside quotation marks.

Let us consider the following string:

<code>('ABCDEFG', 123542, 'XYZ 99,9')</code>
Copy after login

Our goal is to split this string into its component parts using regular expressions in order to obtain the following output:

  • 'ABCDEFG'
  • 123542
  • 'XYZ 99,9'

The key is to recognize commas outside quotation marks. For this we can use the powerful regular expression pattern:

<code>",(?=(?:[^']*'[^']*')*[^']*$)"</code>
Copy after login

Let’s break down this pattern:

  • ": Matches a literal comma.

  • (?=(?:[^']*'[^']*')*[^']*$): This is a lookahead assertion that checks whether a comma is followed by an even number of quotes.

    • (?:[^']*'[^']*')*: Matches any number of pairs of single quotes with the content between them.
    • [^']*$: Matches any character after the last quotation mark, but not including the quotation mark.

By using this pattern we can match any comma that is followed by an even number of quotes, effectively ensuring that we only split the string outside of the quotes.

To use this regular expression for splitting we will use the following C# code:

<code class="language-csharp">var result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)");</code>
Copy after login

After executing this code, result will contain an array of strings, each string representing a component of the original string, split according to the desired conditions.

The above is the detailed content of How to Split a String in C# Using Regex to Handle Commas Within Quotes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template