从方括号之间的字符串中提取一组数字

tec*_*ora 0 c#

我需要从字符串中提取票证号码。字符串示例:

string body = "Hello, world welcome. [TKNM-1234] Blah blah [HelpCode-5] Blah blah";
Run Code Online (Sandbox Code Playgroud)

我只需1234要从该字符串中提取。做这个的最好方式是什么。我以前尝试过:

int from = body.IndexOf("[TKNM-") + "[TKNM-".Length;
int to = body.LastIndexOf("]");
ticketNumber = body.Substring(from, to - from);
Run Code Online (Sandbox Code Playgroud)

但这是由于TKNM之后的其他括号而引起的问题。一切都在C#中。

Mat*_*t.G 5

试试正则表达式: (?<=TKNM-)\d+

演示版

使用Regex.Match