Regexp fails to return matches

C

Chris Beach

Hi there,

I'm attempting to extract a number from a string, and I'm using the
following code:


Regex prRegexp = new Regex( @"Rank_1:\d+:(\d+)" );
MatchCollection matches = prRegexp.Matches( responseText.ToString() );


responseText.ToString() evaluates to "\n\nRank_1:1:4\n\n"

I'm only getting one match back, in the matches collection. The value
of this is the whole string.

I'd expect to get a second match which contains the final digit (4),
but this doesn't appear

Any ideas, folks? I'd appreciate some help on this

Regards

Chris Beach
 
C

Chris Beach

Aha!

I've solved my own problem - I wasn't using the Groups property of the
Match object. The following code works:

Regex prRegexp = new Regex( @"Rank_1:\d+:(\d+)" );
MatchCollection matches = prRegexp.Matches( responseText.ToString() );
if ( matches.Count < 1 || matches[0].Groups.Count < 2 )
throw new Exception( "No Pagerank found for " + url + ". The response
was invalid" );
else
pr = Convert.ToInt32( matches[0].Groups[1].Value );
 
G

Guest

Aha!
I've solved my own problem - I wasn't using the Groups property of the
Match object. The following code works:

Regex prRegexp = new Regex( @"Rank_1:\d+:(\d+)" );
MatchCollection matches = prRegexp.Matches( responseText.ToString() );
if ( matches.Count < 1 || matches[0].Groups.Count < 2 )
throw new Exception( "No Pagerank found for " + url + ". The response
was invalid" );
else
pr = Convert.ToInt32( matches[0].Groups[1].Value );

Find out more about Match, Group and Capture at
http://www.geekswithblogs.net/rahul/archive/2005/08/16/50330.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top