在字符串之前获取数字 C#

问题描述 投票:0回答:1

我正在尝试在 C# 上提取字符串之前的数字。不是字符串之间或之后的数字。字符串之前只有数字。 我需要这些结果。

  • dorosły ==> 0 或“”
  • dziecko 0-3 lata ==> 0 或“”
  • 1-osobowy 四边形 ==> 1
  • 9-osobowy 四边形 ==> 9

我在 C# 上尝试了正则表达式,但得到了“”;

 string test =Regex.Match("1-osobowy", @"/^(\d)-.*/m").Value;
c# regex
1个回答
0
投票

试试这个:

string test = Regex.Match("1-osobowy", @"\d+").Value;
© www.soinside.com 2019 - 2024. All rights reserved.