Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numeric part from string using regex and c#

Tags:

c#

regex

Anyone keen to show me how to get the integer part from this string using c#?

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

I've been into using something like this but didn't get it to work properly

var getNumeric = new Regex(".*([0-9]+)");
like image 717
Eric Herlitz Avatar asked Dec 09 '22 04:12

Eric Herlitz


1 Answers

Your expression could be as simple as \d+, given the assumption that there will be only one number in your input. Still under that assumption, using ToString on the resulting Match will give you what you are looking for.

var regex = Regex.Match("/{localLink:1301}", @"\d+");
var result = regex.ToString();
like image 123
vcsjones Avatar answered Dec 26 '22 19:12

vcsjones