473,787 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegEx problem

Hi,
I have problems with following code and don’t find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
if(text != null && regStr.IsMatch( text))
{
Match m = regStr.Match(te xt);
GroupCollection groups = m.Groups;
number = 0;
for(int i=1;i < groups.Count;i+ +)
{
foreach(Capture c in groups[i].Captures)
{
aArray.Add(c.Va lue.ToString()) ;
number++;
}
}

}

[8,9] : thats working in my aArray I have 8 and 9
[16,5] : OK I have 16 and 5
[16,34] : That is nok I have 3 items in my array 16 and 3 and 4
[16] : that’s is nok I have 2 items in my array 1 and 6

Why m.groups has 3 groups for [16,34]? The same for [16] why m.groups has 2
groups.
I think it must be the last part of my regex expression (\d+). This is one
group even if there are more numbers in it. How can I solve this?

Thanks in advance,
jac

Jun 28 '07 #1
7 2231
"jac" <ja*@discussion s.microsoft.com schrieb im Newsbeitrag
news:00******** *************** ***********@mic rosoft.com...
Hi,
I have problems with following code and don't find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
Why the '?' behine '[,]' ?
That allows to match only part of a number and put the rest in the next
number.
And why the brackets around the comma?
That seems souerfluous to me.

Christof
Jun 28 '07 #2
* jac wrote, On 28-6-2007 17:26:
Hi,
I have problems with following code and don’t find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
if(text != null && regStr.IsMatch( text))
{
Match m = regStr.Match(te xt);
GroupCollection groups = m.Groups;
number = 0;
for(int i=1;i < groups.Count;i+ +)
{
foreach(Capture c in groups[i].Captures)
{
aArray.Add(c.Va lue.ToString()) ;
number++;
}
}

}

[8,9] : thats working in my aArray I have 8 and 9
[16,5] : OK I have 16 and 5
[16,34] : That is nok I have 3 items in my array 16 and 3 and 4
[16] : that’s is nok I have 2 items in my array 1 and 6

Why m.groups has 3 groups for [16,34]? The same for [16] why m.groups has 2
groups.
I think it must be the last part of my regex expression (\d+). This is one
group even if there are more numbers in it. How can I solve this?

Thanks in advance,
jac

\[(?<number>\d+)( ?:,(?<number>\d +))*\]

should do the trick. Currently there are too many options as both the ,
as well as the whole first group are optional (which they're not).

The new expression reads

find a [
find a number (one or more digits)
optionally find a comma followed by a number
repeat optional group if possible
find a ]

both number are captured in the same named group, which makes it easier
to extract the values:

Match m = regStr.Match(te xt);
foreach (Capture c in m.Groups["number"].Captures)
{
aArray.Add(c.Va lue);
}

number = aArray.Count;

Optionally you could also do a string.Split with '[', ',' and ']' as
separator characters which would probably be faster as well. You can
instruct string.Split to ignore empty groups.

string[] results = "[16,23,1]".Split(new char[] { ',', '[', ']' },
StringSplitOpti ons.RemoveEmpty Entries);
int number = results.Length;

I'd prefer this solution over the regex one.

Jesse
Jun 28 '07 #3
Because I can have 0 or multiple sets of 15,12,5,13, therefore ((\d+)[,]?)
In the set I can have 0 or 1 comma, but I can have the set multiple times
(Example[12,4,56,7,14,25 ,12]) or not and then I think I fall in the last part
of it (example [45])

"Christof Nordiek" wrote:
"jac" <ja*@discussion s.microsoft.com schrieb im Newsbeitrag
news:00******** *************** ***********@mic rosoft.com...
Hi,
I have problems with following code and don't find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");

Why the '?' behine '[,]' ?
That allows to match only part of a number and put the rest in the next
number.
And why the brackets around the comma?
That seems souerfluous to me.

Christof
Jun 28 '07 #4
Hello,

First, very good and detailed answer! (Got a positive rate from me)

But I would prefere the string.Split solution that you also presented.
A quick test with a loop and two timestamps will show you why!

All the best,

Martin

"Jesse Houwing" wrote:
* jac wrote, On 28-6-2007 17:26:
Hi,
I have problems with following code and don’t find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
if(text != null && regStr.IsMatch( text))
{
Match m = regStr.Match(te xt);
GroupCollection groups = m.Groups;
number = 0;
for(int i=1;i < groups.Count;i+ +)
{
foreach(Capture c in groups[i].Captures)
{
aArray.Add(c.Va lue.ToString()) ;
number++;
}
}

}

[8,9] : thats working in my aArray I have 8 and 9
[16,5] : OK I have 16 and 5
[16,34] : That is nok I have 3 items in my array 16 and 3 and 4
[16] : that’s is nok I have 2 items in my array 1 and 6

Why m.groups has 3 groups for [16,34]? The same for [16] why m.groups has 2
groups.
I think it must be the last part of my regex expression (\d+). This is one
group even if there are more numbers in it. How can I solve this?

Thanks in advance,
jac


\[(?<number>\d+)( ?:,(?<number>\d +))*\]

should do the trick. Currently there are too many options as both the ,
as well as the whole first group are optional (which they're not).

The new expression reads

find a [
find a number (one or more digits)
optionally find a comma followed by a number
repeat optional group if possible
find a ]

both number are captured in the same named group, which makes it easier
to extract the values:

Match m = regStr.Match(te xt);
foreach (Capture c in m.Groups["number"].Captures)
{
aArray.Add(c.Va lue);
}

number = aArray.Count;

Optionally you could also do a string.Split with '[', ',' and ']' as
separator characters which would probably be faster as well. You can
instruct string.Split to ignore empty groups.

string[] results = "[16,23,1]".Split(new char[] { ',', '[', ']' },
StringSplitOpti ons.RemoveEmpty Entries);
int number = results.Length;

I'd prefer this solution over the regex one.

Jesse
Jun 28 '07 #5
Thank you, it works nice and it was a very good description how to read a
regex.
"Jesse Houwing" wrote:
* jac wrote, On 28-6-2007 17:26:
Hi,
I have problems with following code and don’t find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
if(text != null && regStr.IsMatch( text))
{
Match m = regStr.Match(te xt);
GroupCollection groups = m.Groups;
number = 0;
for(int i=1;i < groups.Count;i+ +)
{
foreach(Capture c in groups[i].Captures)
{
aArray.Add(c.Va lue.ToString()) ;
number++;
}
}

}

[8,9] : thats working in my aArray I have 8 and 9
[16,5] : OK I have 16 and 5
[16,34] : That is nok I have 3 items in my array 16 and 3 and 4
[16] : that’s is nok I have 2 items in my array 1 and 6

Why m.groups has 3 groups for [16,34]? The same for [16] why m.groups has 2
groups.
I think it must be the last part of my regex expression (\d+). This is one
group even if there are more numbers in it. How can I solve this?

Thanks in advance,
jac


\[(?<number>\d+)( ?:,(?<number>\d +))*\]

should do the trick. Currently there are too many options as both the ,
as well as the whole first group are optional (which they're not).

The new expression reads

find a [
find a number (one or more digits)
optionally find a comma followed by a number
repeat optional group if possible
find a ]

both number are captured in the same named group, which makes it easier
to extract the values:

Match m = regStr.Match(te xt);
foreach (Capture c in m.Groups["number"].Captures)
{
aArray.Add(c.Va lue);
}

number = aArray.Count;

Optionally you could also do a string.Split with '[', ',' and ']' as
separator characters which would probably be faster as well. You can
instruct string.Split to ignore empty groups.

string[] results = "[16,23,1]".Split(new char[] { ',', '[', ']' },
StringSplitOpti ons.RemoveEmpty Entries);
int number = results.Length;

I'd prefer this solution over the regex one.

Jesse
Jun 28 '07 #6
* Martin# wrote, On 28-6-2007 18:40:
Hello,

First, very good and detailed answer! (Got a positive rate from me)
Thank you :)
But I would prefere the string.Split solution that you also presented.
A quick test with a loop and two timestamps will show you why!
I hadn't tested, but my guess is that it's a major difference. Regex can
do beautiful things, but isn't the best tool for every problem. As I
said before: I'd prefer this solution over the regex one. It's both
easier to read, and faster. The only problem is that it doesn't validate
the input while the regex would do that for you.

I'm not sure if a int.TryParse would impact the loop you tried enough to
make is slower than a regex though, my guess is that it's still faster
than a regex.

Jesse
All the best,
and to you.

Jesse

>
Martin

"Jesse Houwing" wrote:
>* jac wrote, On 28-6-2007 17:26:
>>Hi,
I have problems with following code and don’t find the bug :

// Set [8,9,54]
ArrayList aArray = new ArrayList();
regStr = new Regex(@"\[(?:(\d+)[,]?)*(\d+)\]");
if(text != null && regStr.IsMatch( text))
{
Match m = regStr.Match(te xt);
GroupCollection groups = m.Groups;
number = 0;
for(int i=1;i < groups.Count;i+ +)
{
foreach(Capture c in groups[i].Captures)
{
aArray.Add(c.Va lue.ToString()) ;
number++;
}
}

}

[8,9] : thats working in my aArray I have 8 and 9
[16,5] : OK I have 16 and 5
[16,34] : That is nok I have 3 items in my array 16 and 3 and 4
[16] : that’s is nok I have 2 items in my array 1 and 6

Why m.groups has 3 groups for [16,34]? The same for [16] why m.groups has 2
groups.
I think it must be the last part of my regex expression (\d+). This is one
group even if there are more numbers in it. How can I solve this?

Thanks in advance,
jac

\[(?<number>\d+)( ?:,(?<number>\d +))*\]

should do the trick. Currently there are too many options as both the ,
as well as the whole first group are optional (which they're not).

The new expression reads

find a [
find a number (one or more digits)
optionally find a comma followed by a number
repeat optional group if possible
find a ]

both number are captured in the same named group, which makes it easier
to extract the values:

Match m = regStr.Match(te xt);
foreach (Capture c in m.Groups["number"].Captures)
{
aArray.Add(c.Va lue);
}

number = aArray.Count;

Optionally you could also do a string.Split with '[', ',' and ']' as
separator characters which would probably be faster as well. You can
instruct string.Split to ignore empty groups.

string[] results = "[16,23,1]".Split(new char[] { ',', '[', ']' },
StringSplitOpt ions.RemoveEmpt yEntries);
int number = results.Length;

I'd prefer this solution over the regex one.

Jesse
Jun 28 '07 #7
"jac" <ja*@discussion s.microsoft.com schrieb im Newsbeitrag
news:86******** *************** ***********@mic rosoft.com...
Because I can have 0 or multiple sets of 15,12,5,13, therefore
((\d+)[,]?)
In the set I can have 0 or 1 comma, but I can have the set multiple times
(Example[12,4,56,7,14,25 ,12]) or not and then I think I fall in the last
part
of it (example [45])
But the 45 would simply be the last number, wich is allready in the RegEx
and the privious group, with the comma will be matched zero times.
Actually that's the cause of the fault, the the first part can match, even
if there is no comma.

Christof
Jun 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2087
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
4
9771
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 numeric value according to an arbitrary regular expression.
7
2619
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
6
4800
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
17
3980
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
3
2119
by: jg | last post by:
I made a mistake somewhere in my vb code and I look, check and read against the articles and help on regex, I still can't find the mistake I made. I know my test string and the test patterns works, because I used on a vs. script to check. I also believe I foolwed followed the regex syntax for dotnet. here is the source code for the function and testing Public Function regtest(ByVal StringIn As String, ByVal patrn As
6
4857
by: Talin | last post by:
I've run in to this problem a couple of times. Say I have a piece of text that I want to test against a large number of regular expressions, where a different action is taken based on which regex successfully matched. The naive approach is to loop through each regex, and stop when one succeeds. However, I am finding this to be too slow for my application -- currently 30% of the run time is being taken up in the regex matching. I thought...
16
2254
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string) pattern search functions. Where performance is an issue you can alway write your own specialized routine of course. However, for the occasional pattern search where performance isn't an issue, would most seasoned .NET developers rely on "Regex" and...
1
12213
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.