473,503 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1326
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9700
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
2593
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
3
1472
by: dje | last post by:
Regex regex = new Regex("//"); Match match; match = regex.Match(this.LastName.Text); if(!match.Success) { sb.Append("Last name invalid. "); returnValue=false; } with the input "jones",...
2
1198
by: George Durzi | last post by:
Consider the following HTML snippet. I want to extract the section shown below. <!-- some html --> <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0><?xml version=1.0 encoding=UTF-16?> **...
6
1761
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
7
2219
by: Mike Labosh | last post by:
I have the following System.Text.RegularExpressions.Regex that is supposed to remove this predefined list of garbage characters from contact names that come in on import files : Dim...
4
2621
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
6
9649
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
10
1849
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 ...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.