Regex.Split() on comma, space or semi-colon delimitted string

Regex.Split() on comma, space or semi-colon delimitted string

To split a string on multiple delimiters (such as comma, space, or semi-colon), you can use a regular expression with the Regex.Split method in C#.

Here's an example of how you can use Regex.Split to split a string on comma, space, or semi-colon:

using System; using System.Text.RegularExpressions; public class Program { static void Main(string[] args) { // Define a string with multiple delimiters string input = "apple,banana;cherry orange kiwi"; // Split the string using a regular expression with comma, space, or semi-colon as delimiters string[] words = Regex.Split(input, @"[,; ]+"); // Print the resulting words foreach (string word in words) { Console.WriteLine(word); } } } 

In this example, we define a string input that contains multiple delimiters (comma, space, and semi-colon). We then use Regex.Split to split the string on one or more occurrences of the delimiters using a regular expression that matches any combination of commas, semicolons, or spaces. Finally, we print the resulting words using a foreach loop.

Note that the regular expression [,; ]+ matches one or more occurrences of commas, semicolons, or spaces. If you need to match other delimiters or patterns, you can modify the regular expression accordingly.

Examples

  1. "C# Regex.Split on comma-delimited string"

    • Description: Find examples of using Regex.Split to split a comma-delimited string in C#.
    // Regex.Split on comma-delimited string string inputString = "one,two,three,four"; string[] result = Regex.Split(inputString, @",\s*"); 
  2. "C# Regex.Split on space-delimited string"

    • Description: Explore how to use Regex.Split to split a space-delimited string in C#.
    // Regex.Split on space-delimited string string inputString = "word1 word2 word3"; string[] result = Regex.Split(inputString, @"\s+"); 
  3. "C# Regex.Split on semi-colon delimited string"

    • Description: Learn how to split a semi-colon delimited string using Regex.Split in C#.
    // Regex.Split on semi-colon delimited string string inputString = "item1;item2;item3"; string[] result = Regex.Split(inputString, @";\s*"); 
  4. "C# Regex.Split on mixed delimiters (comma, space, semi-colon)"

    • Description: Find examples of using Regex.Split to handle mixed delimiters (comma, space, semi-colon) in C#.
    // Regex.Split on mixed delimiters (comma, space, semi-colon) string inputString = "one, two; three four"; string[] result = Regex.Split(inputString, @",|\s|;"); 
  5. "C# Regex.Split on whitespace-separated string"

    • Description: Explore how to use Regex.Split to split a whitespace-separated string in C#.
    // Regex.Split on whitespace-separated string string inputString = "word1 word2 word3"; string[] result = Regex.Split(inputString, @"\s+"); 
  6. "C# Regex.Split on tab-delimited string"

    • Description: Learn how to split a tab-delimited string using Regex.Split in C#.
    // Regex.Split on tab-delimited string string inputString = "data1\tdata2\tdata3"; string[] result = Regex.Split(inputString, @"\t"); 
  7. "C# Regex.Split on multiple delimiters with quantifier"

    • Description: Find examples of using Regex.Split with a quantifier to handle multiple delimiters in C#.
    // Regex.Split on multiple delimiters with quantifier string inputString = "one, two; three four"; string[] result = Regex.Split(inputString, @"[,;\s]+"); 
  8. "C# Regex.Split on pipe (|) delimited string"

    • Description: Explore how to split a pipe (|) delimited string using Regex.Split in C#.
    // Regex.Split on pipe (|) delimited string string inputString = "item1|item2|item3"; string[] result = Regex.Split(inputString, @"\|\s*"); 
  9. "C# Regex.Split on multiple consecutive delimiters"

    • Description: Learn how to handle multiple consecutive delimiters using Regex.Split in C#.
    // Regex.Split on multiple consecutive delimiters string inputString = "word1,,,word2 ;;; word3"; string[] result = Regex.Split(inputString, @",+|;+"); 
  10. "C# Regex.Split on delimited string with optional spaces"

    • Description: Find examples of using Regex.Split to handle a delimited string with optional spaces in C#.
    // Regex.Split on delimited string with optional spaces string inputString = "item1 , item2; item3"; string[] result = Regex.Split(inputString, @"\s*[,;]\s*"); 

More Tags

android-jetpack java-platform-module-system xml-namespaces backwards-compatibility request-headers percentage devicetoken pyuic boto aspectj

More C# Questions

More Cat Calculators

More Biology Calculators

More Stoichiometry Calculators

More Financial Calculators