简体   繁体   中英

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

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 "http://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)}

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