Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changed behavior in RegEx.Split after upgrade of .NET framework

Tags:

regex

split

A client upgraded their systems and they started to report bugs in the output. Apparently, a string split before resulted in the following.

"a-b-c"   ->   {"a", "b", "c"}

Now, however, they get this.

"a-b-c"   ->   {"a", "-", "b", "-", "c"}

I've checked intellisense but as far I can tell, there's no option for turning on/off the inclusion of separators. How can one tackle this easily?

The best suggestion I have off the top of my head is to split using regex and then where it using link with the matching condition of regex. Seems redundant, though...

The current version is 4.5. Before, they had something ooold, like 2.0 or something.

like image 864
Konrad Viltersten Avatar asked Oct 21 '22 10:10

Konrad Viltersten


1 Answers

The behaviour of .NET 4.5 is correct.

The contents of capturing groups are added to the split result. Therefore, Regex.Split("a-b-c", "(-)"); will add the dashes to the array.

Use Regex.Split("a-b-c", "-"); instead.

like image 163
Tim Pietzcker Avatar answered Oct 24 '22 03:10

Tim Pietzcker