0

I want to check whether an URL matches a pattern like: http://the.site.com/some/path/1234567 and simultaneously extract the last number from it.

If I do this so:

Match m = Regex.Match(url, "^http://the.site.com/some/path/(?<picid>.*?)$");
if (m.Success)
{
    log(string.Format("New download. Id={0}", m.Groups["picid"].Value));
}

it returns 2 groups. One contains http://the.site.com/some/path/1234567, the other 1234567. How to change the regex to get only one capture - the number?

4

3 回答 3

4

You can use the regex flag RegexOptions.ExplicitCapture

Usage :

Regex.Match(url, "^http://the.site.com/some/path/(?<picid>.*?)$", RegexOptions.ExplicitCapture);
于 2013-10-29T13:21:47.903 回答
0

By your example you need to check if the string match and get the last digit from the string in your example is 7 do this:

            Match m = Regex.Match(url, @"^http://the.site.com/some/path/\d+$");
            if (m.Success)
            {
                int y = int.Parse(m.Value[m.Value.Length - 1].ToString());
            }
于 2013-10-29T13:32:11.917 回答
0

The Following Regex should capture only the number...

(?<=http://the.site.com/some/path/)(?<picid>.*?)$
于 2013-10-29T19:55:08.823 回答