开发者

Searching a String for a certain thing, then removing up to a certain point to a list

开发者 https://www.devze.com 2023-02-21 23:58 出处:网络
Im working on an Automatic Downloader of sorts for personal use, and so far I have managed to set up the program to store the source of the link provided into a string, the links to the downloads are

Im working on an Automatic Downloader of sorts for personal use, and so far I have managed to set up the program to store the source of the link provided into a string, the links to the downloads are written in plain text in the source, So what I need to be able to do, is search a string for say "htt开发者_如何学Pythonp://media.website.com/folder/" and have it return all occurences to a list? the problem is though, I also need the unique id given for each file after the /folder/" to be stored with each occurence of the above, Any ideas? Im using Visual C#.

Thanks!!!

Steven


Maybe something like this?

        Dictionary<string, string> dictionary = new Dictionary<string, string>();

        string searchText = "Text to search here";
        string textToFind = "Text to find here";
        string fileID = "";

        bool finished = false;
        int foundIndex = 0;

        while (!finished)
        {
            foundIndex = searchText.IndexOf(textToFind, foundIndex);

            if (foundIndex == -1)
            {
                finished = true;
            }
            else
            {
                //get fieID, change to whatever logic makes sense, in this example
                //it assumes a 2 character identifier following the search text
                fileID = searchText.Substring(foundIndex + searchText.Length, 2);

                dictionary.Add(fileID, textToFind);
            }
        }


use Regex to get the matches, that will give you a list of all the matches. Use wildcards for the numeric value that will differ between matches, so you can parse for it.

I'm not great with Regex, but it'd be something like,

Regex.Match(<your string>,@"(http://media.website.com/folder/)(d+)")


Or

var textToFind = "http://media.website.com/folder/";
var ids = from l in listOfUrls where l.StartsWith(textToFind) select new { RawUrl = l, ID=l.Substring(textToFind.Length)}
0

精彩评论

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

关注公众号