C#正则表达式匹配字符串末尾的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1103803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:20:01  来源:igfitidea点击:

C# Regular Expression To Match Number at end of a string

c#

提问by AJM

I have a string that ends with _[a number] e.g. _1 _12 etc etc.

我有一个以 _[a number] 结尾的字符串,例如 _1 _12 等。

I'm looking for a regular expression to pull out this number

我正在寻找一个正则表达式来提取这个数字

采纳答案by Andrew Hare

Try this:

尝试这个:

(\d+)$

Here is an example of how to use it:

以下是如何使用它的示例:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        Regex regex = new Regex(@"(\d+)$", 
            RegexOptions.Compiled | 
            RegexOptions.CultureInvariant);

        Match match = regex.Match("_1_12");

        if (match.Success)
            Console.WriteLine(match.Groups[1].Value);
    }
}

回答by Canopus

Try

尝试

_(\d+)$