简体   繁体   中英

How to get specific value from Regex match grouping in C#

I'm working on getting the screen_name and tweetid from the Twitter urls based on the regular expression group matching.

(http(s)?:\/\/)(?:www.)?twitter\.com\/@?(#!\/)?(?<screenname>[a-zA-Z0-9_]{1,15})(?:\/status(?:es)?\/)?(?<tweetid>\d+)?

I was able to get the Twitter screen_name and tweetid on the following links:

However, I could not get the correct screen_name and tweetid on the following links:

I tried some changes on the regular expression but failed to make it works on the above links.

This are the codes I used to process this the regular expression.

Regex test = new Regex(@"(http(s)?:\/\/)(?:www.)?twitter\.com\/@?(#!\/)?(?<screenname>[a-zA-Z0-9_]{1,15})(?:\/status(?:es)?\/)?(?<tweetid>\d+)?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var matches = test.Matches("https://twitter.com/BeastsMovieUK/status/1042682155590197248");
Match m = null;
if (matches.Count > 0)
{
    foreach (Match match in matches)
    {
        if (match.Groups["tweetid"].Success)
        {
            m = match;
            Console.WriteLine("tweetid: {0}", m);
            break;
        }

        if (match.Groups["screenname"].Success)
        {
            m = match;
            Console.WriteLine("screenname: {0}", m);
        }
    }
}
Console.WriteLine("tweetid result: {0}", m.Groups["tweetid"]);
Console.WriteLine("screenname result: {0}", m.Groups["screenname"]);

This is my working sample: https://dotnetfiddle.net/wPoCSY

If your two URL's are always going to be in the same format you could always just use Split on them. Here's a working example:

https://dotnetfiddle.net/Dda0if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM