Get numeric part from string using regex and c #

Anyone who wants to show me how to get the integer part from this line using C #?

string url = "/{localLink:1301}"; 

I used something like this, but could not work normally

 var getNumeric = new Regex(".*([0-9]+)"); 
+4
source share
3 answers

Your expression may be as simple as \d+ , given the assumption that there will be only one number in your input. Still in this assumption, using ToString in the resulting match, you will get what you are looking for.

 var regex = Regex.Match("/{localLink:1301}", @"\d+"); var result = regex.ToString(); 
+11
source

Your .* Greedy. do you need either .*? or [^\d]*

 new Regex( ".*?(\d+)" ); 
+2
source

you can try this ....

 string output = new string(input.ToCharArray().Where(c => char.IsDigit(c)).ToArray()); 
+1
source

Source: https://habr.com/ru/post/1379864/


All Articles