Steven
Steven

Reputation: 1243

Regular expression question (C#)

How do I write a regular expression to match (_Rev. n.nn) in the following filenames (where n is a number):

Thanks

Upvotes: 1

Views: 99

Answers (3)

FlyingStreudel
FlyingStreudel

Reputation: 4464

Should capture versions >9 Edit: Fixed

string captureString = "abc123butts_Rev. 1.00";
Regex reg = new Regex(@"(.(?!_Rev))+\w_Rev\. (?<version>\d+\.\d+)");
string version = reg.Match(captureString).Groups["version"].Value;

Upvotes: 1

Marty Neal
Marty Neal

Reputation: 9563

Building off of @leppie's answer (give him the green check not me), you can extract the numbers from your regex match by putting parens around the \d's.

Regex foo = new Regex(@"_Rev\.\s(\d)\.(\d\d)$");
GroupCollection groups = foo.Match("Filename_Rev. 1.00").Groups;
string majorNum = groups[1].Value;
string minorNum = groups[2].Value;
System.Console.WriteLine(majorNum);
System.Console.WriteLine(minorNum);

Upvotes: 0

leppie
leppie

Reputation: 117350

The following should work (for the whole line):

@"^Filename_Rev\.\s\d\.\d\d$"

Upvotes: 2

Related Questions