473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex Problem:How to use "balancing group definition"?

please give me a demonstration.
thanks
--
Nov 16 '05 #1
1 2984
Fire,
The following example is taken directly from the e-book "Regular
Expressions with .NET" By Dan Appleman. It only cost $14.95 and is available
for download as soon as you pay. It's a very good resource to have.
Jared

using System;
using System.Text.RegularExpressions;

class AdvancedTests
{
[STAThread]
static void Main(string[] args)
{
AdvancedTests.CommaMatching();
Console.ReadLine();
}

public static void CommaMatching()
{
string inputstring = "1 + ((5 + 3)+ 8 + (4 + (7))) + 6";
MatchCollection parenmatches;
Regex rxparens = new Regex(inputstring);
// First find the locations of left parens
parenmatches = Regex.Matches(inputstring, @"\(");

//Regex rx = new Regex(@"\(.*\)");
//Regex rx = new
Regex(@"(?:(?<ltparen>\()|(?<rtparen-ltparen>\))|[^\)\(])*");
Regex rx = new
Regex(@"(?:(?<ltparen>\()|(?<rtparen-ltparen>\))|(?(ltparen)[^\(\)]*|.*))*");

Match m;
GroupCollection gps;
foreach(Match parenmatch in parenmatches)
{
// Perform the match for each left paren found
m = rx.Match(inputstring, parenmatch.Index);
if (m.Success)
{
gps = m.Groups;
Console.WriteLine(m.Value);
int x;
int[] groupnums = rx.GetGroupNumbers();
String[] groupnames = rx.GetGroupNames();
for(x = 0;x<= groupnums.Length - 1;x++)
{
Console.WriteLine(" Group #: " + groupnums[x].ToString() + " name: "
+ groupnames[x] + " value = " + gps[x].Value);
}
}

}

}

}
"FirePhoenix" <Fi*********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
please give me a demonstration.
thanks
--

Nov 16 '05 #2

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

Similar topics

1
17069
by: Tamer Higazi | last post by:
Hi! I have problems creating joins between tables and to limit rows for a specified clolumn. I have 2 tables! employees departments I face the problems with the GROUP BY clause and i don't...
0
450
by: Lilian BRUN | last post by:
I have a table State_history : Id State DateModif object_id 1 A 10/10/01 1 2 B 10/11/01 1 3 B 11/11/01 1 4 B 11/12/01 1 5 C ...
1
5024
by: Son KwonNam | last post by:
When I try <xsl:value-of select="count('/GROUPS/GROUP')"/> on JSTL <x:transform> tag, I got the following error message. org.apache.jasper.JasperException: Can not convert #STRING to a NodeList!...
10
7117
by: Michael Strorm | last post by:
Hi! I've been having problems with a DTD. Having had the Sun XML validator reject a document, I put it through 'xmllint' for more information. 'Xmllint' noted a problem with the DTD itself;...
0
1887
by: Rob | last post by:
I need to allow a user with the default "Full Data User" permissions on the front-end database to relink to a table in a backend to which he has the same permissions. (I'd like to do read, update,...
1
2502
by: CA | last post by:
I am having a problem getting a pointer to an existing running Excel process on a client machine. Here are some details: 1) I have tested out the code on the development machine and it works...
1
1657
by: ASF | last post by:
Is it possible to create a crystal report graph in which a user selects some parameters---a date range for instance--- AND the table field that they wish to "Group By" then hits the submit button...
16
2229
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)...
5
5916
by: Fred Hebert | last post by:
I was thinking of switching to VS2005, so I sent off for and received a 120 evaluation kit. This version was supposed to be the same as the full version, but the key limits you to 120 days. I...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
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
7405
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...
1
7066
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...
0
7504
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
5059
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
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
0
435
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.