Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "cut" out part of a string with a regex?

Tags:

c#

regex

I need to cut out and save/use part of a string in C#. I figure the best way to do this is by using Regex. My string looks like this:

"changed from 1 to 10".

I need a way to cut out the two numbers and use them elsewhere. What's a good way to do this?

like image 881
Cros Avatar asked Nov 13 '08 13:11

Cros


Video Answer


1 Answers

Error checking left as an exercise...

        Regex regex = new Regex( @"\d+" );
        MatchCollection matches = regex.Matches( "changed from 1 to 10" );
        int num1 = int.Parse( matches[0].Value );
        int num2 = int.Parse( matches[1].Value );
like image 138
tvanfosson Avatar answered Sep 22 '22 12:09

tvanfosson