我具有以下功能,可将packageNameunicueId用于Google Play市场或Apple App Store URL:

string GetPackageNameFromUrl(string url)
{
    string res = "";

    string patternAndroid = @"^https:\/\/play\.google\.com\/store\/apps\/details\?id=(.+)\&.+";
    Regex regexpAndroid = new Regex(patternAndroid, RegexOptions.Compiled);

    if (regexpAndroid.IsMatch(url))
    {
        res  = regexpAndroid.Matches(url).FirstOrDefault()?.ToString();
    }

    string patternIos = @"^https:\/\/apps\.apple\.com\/ru\/app\/.+\/id(\d+)\?.+";
    Regex regexpIos = new Regex(patternIos, RegexOptions.Compiled);


    if (regexpIos.IsMatch(url))
    {
        res = regexpIos.Matches(url).ToList().FirstOrDefault()?.ToString();
    }

    return res;
}


游戏市场网址示例:https://play.google.com/store/apps/details?id=com.zhiliaoapp.musically&hl=ru

IOS App Store URL的示例为:https://apps.apple.com/ru/app/%D1%8F%D0%BD%D0%B4%D0%B5%D0%BA%D1%81-%D0%BD%D0%B0%D0%B2%D0%B8%D0%B3%D0%B0%D1%82%D0%BE%D1%80-gps-%D0%BF%D1%80%D0%BE%D0%B1%D0%BA%D0%B8/id474500851?v0=WWW-EURU-ITSTOP100-FREEAPPS&l=ru&ign-mpt=uo%3D4

我需要捕获这个(如果URL是游戏市场URL)com.zhiliaoapp.musically或这个(如果IOS)474500851并将其作为字符串返回。
但是我的函数返回整个给定的URL。我抓不到错误。错误在哪里?

最佳答案

只需获取捕获id参数或id模式的简单正则表达式即可:

(id(\d+)|id=([\w.]+))


正则表达式101与您的示例的链接:https://regex101.com/r/T1K4a7/1

关于c# - 如何在C#正则表达式的帮助下从应用商店中获取唯一的ID或程序包名称或播放市场网址?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57111605/

10-13 07:40