C# Split string using regex expression

c# regex split multiple delimiters
c# regular expression
regex.split c# example
c# regex split keep delimiter
regex split string python
regex split js
regex split string by word
regex split online

Input: String input = "{True,True,False},{False,True,True},{False,True,True}"

Expected output: String[] output = new String[]("True,True,False", "False,True,True", "False,True,True");

I am trying to use Regex.Split, but unsuccessful with the pattern. Any hints?


Here is my version:

using System;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
    public static void Main(string[] args)
    {
        String input = "{True,True,False},{False,True,True},{False,True,True}";

        var pattern = "{(.*?)}";
        var matches = Regex.Matches(input, pattern);

        var output2 = matches
                 .Select(m => m.Groups[1].ToString())
                 .ToList();

        foreach (var o in output2) Console.WriteLine(o);
    }
}

Output

True,True,False
False,True,True
False,True,True

Regex.Split Method (System.Text.RegularExpressions), Strings can be split with the Regex.Split() method as well; it splits a string based on a regular expression pattern. C# split string simple example. The Regex.Split methods are similar to the String.Split method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The count parameter specifies the maximum number of substrings into which the input string is split; the last string contains the unsplit remainder of the string.


You could try:

var result = input
  // Remove sorrounding { and }     
  .Trim(new char[] { '{', '}' })
  // Split by "},{"
  .Split(new string[] { "},{" }, StringSplitOptions.RemoveEmptyEntries);

C# Regex.Split Examples, tring[] res = Regex.Split(str, "\r\n");. The following is the complete code to split a string using Regular Expression in C#. Now use Regex.split to split the string as shown below − tring [] res = Regex.Split (str, "\r "); The following is the complete code to split a string using Regular Expression in C#.


You can trim first and last { } using .TrimStart() and .TrimEnd() respectively and then apply RegEx with below pattern

    string str = "{True,True,False},{False,True,True},{False,True,True}".TrimStart('{').TrimEnd('}');
    var result = Regex.Split(str, @"},{"); // result is an array of string.
    Console.WriteLine(string.Join(" ", result));

Output

True,True,False False,True,True False,True,True

.Net Fiddle

C# split strings in C# with String.Split() and Regex.Split(), The Split method returns sub strings between the instances of the pattern specified. For example: var items = Regex.Split("this is a test", @"\s");. We use Regex.Split to split on all non-digit values in the input string. We then loop through the result strings, with a foreach-loop, and use int.TryParse. int.Parse Input: The input string contains the numbers 10, 20, 40 and 1, and the static Regex.Split method is called with two parameters.


You can use:

string input = "{True,True,False},{False,True,True},{False,True,True}";

var pattern = @"{([A-Za-z,].+?)}";
var yourArray = Regex.Matches(input, pattern);

var Result= yourArray 
         .Select(m => m.Groups[1].ToString())
         .ToList();

foreach(var r in Result) {
   Console.WriteLine(r);
}

Your output :

True,True,False
False,True,True
False,True,True

How to split a string using regular expressions in C#?, The seventeenth part of the Regular Expressions in .NET tutorial looks at the Split method. Similar to the string class's Split method, this� A regex is probably a bit overkill for that - I'd probably use string.Substring to extract the three parts as the code would probably be more readable. To do that with a regex means accessing the Groups, and using Linq to convert the values to an array: string test = "919963452421AC6214BXCCNY0"; Regex re = new Regex(@"(.{12})(.{8})(.{5})");


Splitting a string in C#, Regex.Split separates strings based on a pattern. It handles a delimiter numbers in a string, and then actually parse them into integers for easier usage in a C#� Personally given my lack of regex-fu, I'd do one of the following: Use a simple regex-based Replace to escape any commas inside quotes with something else (i.e. ","). Then you can do a simple string.Split() on the result and unescape each item in the resulting array before you use it. This is yucky.


Splitting Strings with Regular Expressions, Split() method splits the string at a delimiter determined by a Regular Expression instead of a set of characters. When using Regular Expressions you should use� In a regex designed to "split" a string, the regex will necessarily match the character (s) on which to split. That is exactly what you are seeing, and what I would expect. But now you are presenting a different requirement which involves capturing, and perhaps replacing, those items.


C# Regex.Split Examples, You want to split a string using a regular expression. C#. You can use the static call when you process only a small number of strings with the same regular� In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string. 6. public string [] Split (string input) Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.