-
收到一只叮咚
你不需要正则表达式*。简单的子串和删除可以做到这一点。这是我快速提出的一些东西。string test = "Ł9CZIA KUOTA PIV 1,21 SUMA 12,36 otóuka 2 | 0350 |tKasa 1";test = test.Substring(test.IndexOf("SUMA ") + 5);test = test.Remove(test.IndexOf(' ')); 可能会以某种方式简化,但它确实有效。如果您需要结果实际是一种decimal类型,您当然需要转换它。*请注意,这并不能保证您会有一个数字(例如,如果您的输入错误),因此您需要对其进行验证。由于您编辑了您的帖子以添加这样一个事实,即在我发布答案后 SUMA 和数字之间可能有多个单词,因此我不会在这里明确处理。在这种情况下,我认为正则表达式更有意义。
-
莫回无
如果 SUMA 和 number 之间可以有单词,则可以匹配任何字符零次或多次非贪婪.*?,然后在一个组中捕获(\d+,\d+)SUMA.*? (\d+,\d+)string pattern = @"SUMA.*? (\d+,\d+)";string input = @"Ł9CZIA KUOTA PIV 1,21 SUMA test 12,36 otóuka 1,1 2 | 0350 |tKasa 1";Regex r = new Regex(pattern);Match match = r.Match(input); Console.WriteLine(match.Groups[1]); // 12,36
-
哆啦的时光机
使用代码:string str = "Ł9CZIA KUOTA PIV 1,21 SUMA 12,36 otóuka 2 | 0350 |tKasa 1";int index = str.IndexOf("SUMA");if (index > -1){ str = str.Substring(index + 5);// SUMA + SPACE char == 4+1 = 5 int inx = str.IndexOf(" "); if (index > -1) { str = str.Substring(0, inx); Console.WriteLine(str.Trim()); }}