簡體   English   中英

C#正則表達式匹配字符串末尾的數字

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

我有一個以_ [數字]結尾的字符串,例如_1 _12等。

我正在尋找一個正則表達式來提取這個數字

試試這個:

(\d+)$

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

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

嘗試

_(\d+)$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM