简体   繁体   English

正则表达式问题 (C#)

[英]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):如何编写正则表达式以匹配以下文件名中的 (_Rev. n.nn)(其中 n 是数字):

  • Filename_Rev.文件名_Rev. 1.00 1.00
  • Filename_Rev.文件名_Rev. 1.10 1.10

Thanks谢谢

The following should work (for the whole line):以下应该有效(对于整行):

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

Should capture versions >9 Edit: Fixed应捕获版本 >9 编辑:已修复

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

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.基于@leppie 的答案(给他绿色的支票而不是我),您可以通过在\d周围放置括号来从您的正则表达式匹配中提取数字。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM