Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

regex

split

I'm trying to split a string that can either be comma, space or semi-colon delimitted. It could also contain a space or spaces after each delimitter. For example

22222,11111,23232 
OR
22222, 11111, 23232 
OR
22222;     11111; 23232
OR
22222 11111 23232 

Any one of these would produce an array with three values ["22222","11111","23232"]

So far I have var values = Regex.Split("22222, 11111, 23232", @"[\\s,;]+") but this produces an array with the second and third values including the space(s) like so:

["22222"," 11111"," 23232"]
like image 908
bflemi3 Avatar asked Feb 04 '13 14:02

bflemi3


People also ask

How do you split a string by a comma and space?

split(" ,") will split the string only where a space followed by a comma is the separator. Your string does not contain that sequence, hence it is not splitted. "my, tags are, in here". split(", ") with the splitting sequence swapped will at least split your original string in three parts, after each comma-and-space.

Can we use regex in split a string?

You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.

How do you split a string with spaces?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How does regex split work?

Split(Char[]) method, except that Regex. Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The string is split as many times as possible. If no delimiter is found, the return value contains one element whose value is the original input string.


2 Answers

You have two possibilities:

  • Regex.Split
  • String.Split

In this case, you want to split your string by specific delimiters caracters. String.Split has been created for this special purpose. This method will be faster than Regex.Split.

char[] delimiters = new [] { ',', ';', ' ' };  // List of your delimiters
var splittedArray = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
like image 158
Cédric Bignon Avatar answered Oct 13 '22 20:10

Cédric Bignon


You are using an @ symbol for your string, so the "\" is being interpreted as a literal slash. So your character class is actually reading as a "\", an "s", a "," or a ";". Remove the extra slash and it should work as desired:

var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")
like image 43
JDB Avatar answered Oct 13 '22 22:10

JDB