473,774 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about RegEx group capture in C#

There's something for me to learn with this example, i'm sure :)

Given this text:
"[Contact].[Region].[All ContactRegion].[ASIA
PACIFIC].[Japan].[Japan]"
and my first attempt at capture the groups:
"(?:\[)(.+?)(?:\])"
RegExTest gives me what i expect: 6 captured groups: Contact, Region,
All ContractRegion, ASIA PACIFIC, Japan, Japan.

However, with this C# code, i just get 2 capture groups: "[Contact],
Contact":

Regex r = new Regex(@"(?:\[)(.+?)(?:\])");
Match m = r.Match(LongPar ameterValue);
GroupCollection gc = m.Groups; //group count is 2

Am i capturing this incorrectly in C#?

Thanks.
Nov 15 '05 #1
2 5983
Hey,

I haven't done this in a while, but when your matching, I believe you
looking only for the first occurance. You should use Test or at the end of
your match specify /g for global searching, so that it will find all of the
instances in your pattern.

Hope this helps. Just an idea.

Nick Harris, MCSD
http://www.VizSoft.net
"Jose" <jo**********@y ahoo.com> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
There's something for me to learn with this example, i'm sure :)

Given this text:
"[Contact].[Region].[All ContactRegion].[ASIA
PACIFIC].[Japan].[Japan]"
and my first attempt at capture the groups:
"(?:\[)(.+?)(?:\])"
RegExTest gives me what i expect: 6 captured groups: Contact, Region,
All ContractRegion, ASIA PACIFIC, Japan, Japan.

However, with this C# code, i just get 2 capture groups: "[Contact],
Contact":

Regex r = new Regex(@"(?:\[)(.+?)(?:\])");
Match m = r.Match(LongPar ameterValue);
GroupCollection gc = m.Groups; //group count is 2

Am i capturing this incorrectly in C#?

Thanks.

Nov 15 '05 #2
Hi,
[inline]

"Jose" <jo**********@y ahoo.com> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
There's something for me to learn with this example, i'm sure :)

Given this text:
"[Contact].[Region].[All ContactRegion].[ASIA
PACIFIC].[Japan].[Japan]"
and my first attempt at capture the groups:
"(?:\[)(.+?)(?:\])"
You don't need the non-capture groups. Simplified this becomes:
@"\[(.+?)\]"
RegExTest gives me what i expect: 6 captured groups: Contact, Region,
All ContractRegion, ASIA PACIFIC, Japan, Japan.
If you look at the regex there are 2 groups :
- the frist group is implicit (the entire match if there is a match)
- the second group is your (.+?)

It is possible for a capture group to contain more then one captured string.
For this to work, the capture group itself must be repeated and use ^$ to
parse the entire string, like in:
@"^(?:\[(.+?)\]\.?)+$"

If you execute this then there will be 2 groups, but the second group will
contain 6 captures.

Regex r = new Regex( @"^(?:\[(.+?)\]\.?)+$" );
Match m = r.Match(LongPar ameterValue);
foreach (Capture c in m.Groups[1] ) //second group
{
Console.Writeli ne(c.Value); // prints a part
}
Now to make it more complicated, there is another way, which I like more.
Using your original regex : @"\[(.+?)\]"
The problem was that it only matches one part (which is in the second group,
first capture), so another solution would be to repeat the regex for all
parts.

Regex r = new Regex( @"\[(.+?)\]" );
Match m = r.Match(LongPar ameterValue);
while (m.Success)
{
Console.Writeli ne(m.Groups[1].Value); // print a part
// note that this is the same as using m.Groups[1].Captures[0].Value;
m = m.NextMatch();
}
HTH,
Greetings

However, with this C# code, i just get 2 capture groups: "[Contact],
Contact":

Regex r = new Regex(@"(?:\[)(.+?)(?:\])");
Match m = r.Match(LongPar ameterValue);
GroupCollection gc = m.Groups; //group count is 2

Am i capturing this incorrectly in C#?

Thanks.

Nov 15 '05 #3

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

Similar topics

4
2143
by: aeuglein | last post by:
Hello! I have this RegEx: /(+:\/\/+)/i Now, I want to exlude on the end of a String the formats .gif / .jpg / ..png / .exe / .zip / .rar How I can this add to my regex ?
1
1315
by: Jeff Johnson [MVP: VB] | last post by:
Is there any way to retrieve the name of a capture group from the classes provided in the RegularExpressions namespace? GroupCollection implements ICollection and IEnumerable, not IDictionary, so there doesn't seem to be any means to retrieve key values. While I realize this isn't generally needed in a production scenario (you normally KNOW what your groups are called), it is quite necessary if you're building a general regular...
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....
0
1067
by: Hugo Wetterberg | last post by:
Hi, I had a problem with a rather complex regex expression that didn't return the same results in 1.1 and 2.0. I first thought that I could make a utility assambly that referenced the 1.1 version of mscorlib and the use that from the 2.0 application. But that didn't work so I drew the conclusion that the problem was the runtime. That was wrong. I've compiled the regular expression in 1.1 into a physical assembly using...
2
1385
by: eBob.com | last post by:
I am using regular expressions and a particular feature called "capture" (I think) to suck some information out of some html. I could have never come up with this myself but Balena has an example which is very similar to this. The guts of the program is ... Dim i As Integer Dim rgx As Regex Dim Pattern As String = "<td class=td1 width=""35%"">(<b>){0,1}(?<variable>(\w| )+)</td>" + _
11
279
by: proctor | last post by:
hello, i have a regex: rx_test = re.compile('/x()*x/') which is part of this test program: ============ import re
3
1651
by: Ethan Strauss | last post by:
Hi, I have written a regular expression which is supposed to pull a direction (forward or reverse) designation from a file name. Unfortunately, the direction designation can either be the whole word ("Forward" or "Reverse") or just a single letter ("F" or "R") and the rest of the name is not as consistent as I would like.. For example "P1|1_G10_Forward_primer.ab1" or "K8_I1_A01_F.ab1".
1
2723
by: =?Utf-8?B?QWxCcnVBbg==?= | last post by:
I have a regular expression for capturing all occurrences of words contained between {{ and }} in a file. My problem is I need to capture what is between those symbols. For instance, if I have tags such as {{FirstName}}, {{LastName}}, and {{Address}} placed in the file, I need to be able to capture the text strings of FirstName, LastName and Address, respectively. I'm sure it can be done with Regex as easily as finding the locations of...
4
312
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of URLs. (href=("|')http://.www\.thesite\.com/.{1,7}/)(?<filename>.) This generally works, but the problem is some of the image files have ..th.jpg at the end to indicate thumbnails. I want to exclude those. I just want the ones that don't have...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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...
0
9914
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
8937
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...
0
5355
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.