Regex to find trailing numbers
I have a string that looks like:
www.blah.com/asdf/asdf/asdfasedf/123
The string may have a slash followed by numbers, like /123 in开发者_开发问答 the above example.
I want to extract the 123 from the string if it is present.
What would my regex be?
Terminate your regular expression with $ to signify the end of the line.
\/\d+$
To actually extract the number, use:
int number;
var match = Regex.Match(inputString,@"\/(\d+)$");
if(match.Success)
number = int.Parse(match.Groups[1].ToString());
You simply match a group of digits (\d+) and require the string to end after that
(\d+)$
This will match a slash followed by numbers at the end of a string and capture the numbers:
\/(\d*)$
精彩评论