Small bit of regular expression help?

S

sherifffruitfly

Hi,

(If anyone's upset enough, I'll go to a different group. But I am
writing this program in c#, so I don't think it's totally off-topic.)

I'm trying to dig numbers out of a text file, and I've got the
following regex:

Conforming\s3/1\sYr\sARMs\s*\d{1,2}.\d\d

This matches perfectly for what I'm after (using Expresso to test). But
what I want is that last bit assigned to a variable - the \d{1,2}.\d\d
part. How do I do that?

Thanks for any enlightenment,

cdj
 
M

Marc Gravell

You need a group (named or by number; I've gone for named):

Regex re = new
Regex(@"Conforming\s3/1\sYr\sARMs\s*(?<digit>\d{1,2}.\d\d)");
Match match = re.Match("Conforming 3/1 Yr ARMs 12.31");
string digit = match.Groups["digit"].Value;
Console.WriteLine(digit);

That do?

Marc
 
S

sherifffruitfly

Marc said:
You need a group (named or by number; I've gone for named):

Regex re = new
Regex(@"Conforming\s3/1\sYr\sARMs\s*(?<digit>\d{1,2}.\d\d)");
Match match = re.Match("Conforming 3/1 Yr ARMs 12.31");
string digit = match.Groups["digit"].Value;
Console.WriteLine(digit);

That do?

Marc

More or less - all I wanted was to know *how* to "extract" part of a
matched regex to a variable - and you've shown that perfectly clearly.

Thanks!

cdj
 

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