开发者

Regex to find trailing numbers

开发者 https://www.devze.com 2022-12-15 10:23 出处:网络
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 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*)$
0

精彩评论

暂无评论...
验证码 换一张
取 消