要提取字符串中的数字,可以使用正则表达式来实现。以下是一个示例代码,演示如何使用正则表达式从字符串中提取数字:\n\ncsharp\nusing System;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n static void Main()\n {\n string input = "Ms_GxName2";\n string pattern = @"\d+";\n\n Match match = Regex.Match(input, pattern);\n if (match.Success)\n {\n string number = match.Value;\n Console.WriteLine(number);\n }\n else\n {\n Console.WriteLine("No match found.");\n }\n }\n}\n\n\n在上面的代码中,我们使用了\d+正则表达式模式来匹配一个或多个数字。如果找到匹配项,我们将提取的数字打印出来;否则,打印"No match found."。运行代码后,将输出"2"。

C# 正则表达式提取字符串中的数字 - 如何从

原文地址: https://www.cveoy.top/t/topic/qC4w 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录