PC Review


Reply
Thread Tools Rate Thread

Counting regex expressions

 
 
trashman.horlicks@btinternet.com
Guest
Posts: n/a
 
      19th Jan 2007
Hi,
I'm getting into a tangle with Regex in C#.

If I had an expression like abc[1].def[2].ghi[3] etc.

How do I count how many instances there are of "[ ]" and "."

Thanks a lot!

TH

 
Reply With Quote
 
 
 
 
Jon Shemitz
Guest
Posts: n/a
 
      19th Jan 2007
(E-Mail Removed) wrote:

> If I had an expression like abc[1].def[2].ghi[3] etc.
>
> How do I count how many instances there are of "[ ]" and "."


Use the regex @" \[ \s* \d+ \s* \] " with the IgnorePatternWhitespace
option; call Matches; and read the Count property.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
 
Reply With Quote
 
trashman.horlicks@btinternet.com
Guest
Posts: n/a
 
      19th Jan 2007

Jon Shemitz wrote:
> (E-Mail Removed) wrote:
>
> > If I had an expression like abc[1].def[2].ghi[3] etc.
> >
> > How do I count how many instances there are of "[ ]" and "."

>
> Use the regex @" \[ \s* \d+ \s* \] " with the IgnorePatternWhitespace
> option; call Matches; and read the Count property.
>
> --
>
> .NET 2.0 for Delphi Programmers
> www.midnightbeach.com/.net


Could I do it like this?

// Assume Key is of the form a.b.c.d.e.f

Match n = Regex.Match( key, @"\." );

int noOfMatches = n.Count;

 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      19th Jan 2007
(E-Mail Removed) wrote:

> Could I do it like this?
>
> // Assume Key is of the form a.b.c.d.e.f
>
> Match n = Regex.Match( key, @"\." );
>
> int noOfMatches = n.Count;


Not quite - the Match object represents a single match, and doesn't
have a Count property. You want the Matches method, not the Match
method:

int NumberOfDots = Regex.Matches(Key, @"\.").Count;

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions (RegEx) chris fellows Microsoft Dot NET 2 5th Feb 2008 12:24 PM
regex expressions Bert Microsoft VB .NET 7 23rd Dec 2006 09:27 PM
Common Regex.IsMatch expressions Dean Slindee Microsoft VB .NET 3 23rd Apr 2005 05:23 PM
Regular Expressions RegEx - Need help with syntax Thomas Microsoft VB .NET 0 30th Apr 2004 02:59 PM
Regex Class (Regular Expressions) Demetri Microsoft C# .NET 4 7th Aug 2003 09:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:15 PM.