471,330 Members | 1,490 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Regex, what is wrong

Hello, what is wrong with this code, I want to divide string to pieces 4
chars length.

string tekst = "divide string to 4 chars pieces";
Regex regex = new Regex("(.{1,4})");
string[] substrings = regex.Split(tekst);

but instead of array with:
"divi","de s","trin" etc..
i have
"","divi","","de s","","trin", etc..

What is wrong?
Sep 8 '08 #1
8 1238
ad******@antyspam.pl wrote:
Hello, what is wrong with this code, I want to divide string to pieces 4
chars length.

string tekst = "divide string to 4 chars pieces";
Regex regex = new Regex("(.{1,4})");
string[] substrings = regex.Split(tekst);

but instead of array with:
"divi","de s","trin" etc..
i have
"","divi","","de s","","trin", etc..

What is wrong?
There is nothing wrong. You are splitting the string with four
characters as separator. That means that the first separator is found at
the first character, the second separator is found at the fifth
character, and so on. All you find in the string is separators, and no
text between them.

The result is the text separated by the separators, and the separators
between them. As the separator matches anything, the strings separated
by the separators all become empty strings.

It's similar to doing this:

",,,".Split(',')

the result would be an array with four empty strings, as the string only
consists of separators.

--
Göran Andersson
_____
http://www.guffa.com
Sep 8 '08 #2
Göran Andersson pisze:
There is nothing wrong. You are splitting the string with four
characters as separator. That means that the first separator is found at
[cut]
the result would be an array with four empty strings, as the string only
consists of separators.
Thanks.

So, is there any way to split string into numbered pieces by using Regex
, instead of using Substring etc...? There is no String.Split(Int) in
framework
Sep 8 '08 #3
Why not just use Substring? For example (in this case using C# 3.0
extension methods, but would work just as well without them...):

Marc

using System;
using System.Collections.Generic;
using System.Linq;
static class Program
{
static void Main()
{

string test = "divide string to 4 chars pieces";
foreach (string s in test.Split(4))
{
Console.WriteLine(s);
}
// or alternatively...
string[] split = test.Split(4).ToArray();
}
public static IEnumerable<stringSplit(this string value, int size)
{
int offset = 0, remaining = value.Length;
while (remaining >= size)
{
yield return value.Substring(offset, size);
offset += size;
remaining -= size;
}
if (remaining 0) yield return value.Substring(offset);
}
}
Sep 8 '08 #4
you might want to add:

if (value == null) throw new ArgumentNullException("value");
if (size <= 0) throw new ArgumentOutOfRangeException("size",
"Size must be positive");
Sep 8 '08 #5
Marc Gravell pisze:
Why not just use Substring?
Because i don't want :) , i'm curious how to do it with Regex.
Sep 8 '08 #6
Hi Mark.

Not to want to be picky or anything but to make this code more robust
I think you will also need to account for Unicode strings that are not
normalized.

For example, things like combining marks such as Unicode code point U
+00E0 (http://www.fileformat.info/info/unic...00e0/index.htm)
that can also be represented as U+0061 U+0300.

Simply splitting the string may yield unexpected results.

I have no doubt that you already knew this and that the sample you
posted is just to illustrate your point but I thought I would just
point the issue out so that the original poster is aware of it (in
case he/she is not already).

Of course, I may be totally wrong about what I my comment so if that
is the case just ignore me!!!!

Cheers.


On Sep 8, 5:05*am, Marc Gravell <marc.grav...@gmail.comwrote:
Why not just use Substring? For example (in this case using C# 3.0
extension methods, but would work just as well without them...):

Marc

using System;
using System.Collections.Generic;
using System.Linq;
static class Program
{
* * *static void Main()
* * *{

* * * * *string test = "divide string to 4 chars pieces";
* * * * *foreach (string s in test.Split(4))
* * * * *{
* * * * * * *Console.WriteLine(s);
* * * * *}
* * * * *// or alternatively...
* * * * *string[] split = test.Split(4).ToArray();
* * *}
* * *public static IEnumerable<stringSplit(this string value, intsize)
* * *{
* * * * *int offset = 0, remaining = value.Length;
* * * * *while (remaining >= size)
* * * * *{
* * * * * * *yield return value.Substring(offset, size);
* * * * * * *offset += size;
* * * * * * *remaining -= size;
* * * * *}
* * * * *if (remaining 0) yield return value.Substring(offset);
* * *}

}- Hide quoted text -

- Show quoted text -
Sep 8 '08 #7
ad******@antyspam.pl wrote:
Marc Gravell pisze:
>Why not just use Substring?

Because i don't want :) , i'm curious how to do it with Regex.
Then use Match instead of Split.
Sep 8 '08 #8
but to make this code more robust
I think you will also need to account for Unicode strings that are not
normalized.
A fair point - but this same problem will affect most simple
mechanisms to split the string (including the original regex, it would
seem).

Marc
Sep 9 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by bill tie | last post: by
3 posts views Thread by dje | last post: by
2 posts views Thread by George Durzi | last post: by
6 posts views Thread by tshad | last post: by
7 posts views Thread by Mike Labosh | last post: by
6 posts views Thread by Gary Bond | last post: by
10 posts views Thread by bullockbefriending bard | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.