How to extract two numbers from string with a regex in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;
 
class Program
{
    static void Main() {
        string str = "c# 12 java 17";
         
        Regex regex = new Regex( @"\d+" );
        MatchCollection matches = regex.Matches(str);
        
        int num1 = int.Parse(matches[0].Value);
        int num2 = int.Parse(matches[1].Value);
        
        Console.WriteLine(num1);
        Console.WriteLine(num2);
    }
}
 
 
 
 
/*
run:
  
12
17
  
*/

 



answered Jun 24, 2024 by avibootz

Related questions

...