从字符串中获取最后一组数字的正则表达式

4
"abc_d1.txt" should get 0
"abc_d1_2.txt" should get 2
"abc_d1_14.txt" should get 14
"abc_d12_x2_156.txt" should get 156

这是我目前所做的,但我没有得到正确的结果。
  string y = "tester_yg1.txt";
  string pattern = @"(\d+)(?!.*\d)";
  Regex rg = new Regex(pattern);
  var z = rg.Match(fullFileName).Value;
  Console.WriteLine($"z is {z}");

1
"abc_d1.txt" 应该得到 0 - 正则表达式无法匹配字符串中不存在的字符!您需要将空匹配转换为 0。 - Poul Bak
@PoulBak 是的,我也这么认为。 - user10860402
4个回答

1

另一个选项是使用单个正向先行断言。

(?<=_[^\W\d_]\d+_?)\d*(?=\.\w+$)

解释

  • (?<= 正向先行断言,断言左侧是
    • _[^\W\d_]\d+_? 匹配 _ ,一个非数字或下划线的词字符和1+数字后跟一个可选的 _
  • ) 关闭先行断言
  • \d* 匹配0个或多个数字(也可以获取没有数字时的位置)
  • (?= 正向后行断言,断言右侧是
    • \.\w+$ 匹配一个点 . 和 1 个或多个单词字符一直到字符串结尾
  • ) 关闭后行断言

.NET正则表达式演示

enter image description here


0

如果使用.NET,为什么不直接使用正则表达式\d+并执行regex.Matches(..).LastOrDefault();呢?


0
你可以使用这个正则表达式:
@"(?<=_)\d+(?=\.\w+)|(?<!_\d+)(?=\.\w+)"

解释:

(?<=_) 向后查找下划线

\d+ 匹配一个或多个数字

(?=\.\w+) 向前查找点后面跟着一个或多个单词字符(扩展名)

| 或者

(?<!_\d+) 向后查找不是下划线后面跟着一个或多个数字

(?=\.\w+) 向前查找点后面跟着一个或多个单词字符(扩展名)

当没有数字时,最后一部分将返回一个空匹配。

然后你需要在代码中将空匹配转换为0

更新:

要删除数字,您可以使用此Regex,现在调用Replace代替:

string pattern1 =  @"_\d+(?=\.\w+)|(?<!_\d+)(?=\.\w+)";
Regex rg1 = new Regex(pattern1);
var z = rg1.Replace(y, "");
string z1  = Path.GetFilename(z);

现在z1应该包含已删除编号和扩展名的文件名。当没有编号时,它将返回没有扩展名的文件名。


谢谢。如果您不介意,我可以再问一个问题吗?例如:"abc_d1_14.txt",如何得到相反的结果呢?结果应该是"abc_d1",而在"abc_d12_x2_156.txt"中的结果应该是"abc_d12_x2"。 - user10860402

0

使用

(?<![^\W_])\d+(?=\.\w+$)

查看证明

说明

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    [^\W_]                   any character except: non-word
                             characters (all but a-z, A-Z, 0-9, _),
                             '_'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接