Find the last occurrence of a string by specific keyword RRS feed

  • Question

  • For example the text file length of lines updates every few seconds, I would like to find the last occurrence for a specific string 

    Here is a sample of how the text file looks. ( unknown length as in constantly being written by the server)

    Plan: ofeachday: - time: Noon [accepted]

    The "time" variable will change such as "Noon" will be something else, and I don't what what will it be.

    So I would like to read it to the console. Any suggestions are appreciated.

    • Edited by HisKingdom Saturday, July 6, 2019 1:42 PM
    Friday, July 5, 2019 9:36 AM

All replies

  • How large is the text file and how are you creating the file? Any other details you have left out?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, July 5, 2019 9:44 AM
  • Probably need a few examples of your sample data. You could write a regex and then examine the MatchCollection. And if you have Matches, it would be the last one.

    Is your example(s) like so?

    Plan: Monday: - time: 12:00AM?
    Plan: Tuesday: - time: 1:30PM?

    Regex rx = new Regex(@"Plan:.*?time:.*?[AP]M);
    MatchCollection mc = rx.matches("your text file");

    if (mc.Count > 0)
        //Get the last one    

    I don't have Visual Studio in front of me, but something along those lines.

    Friday, July 5, 2019 2:07 PM
  • To search for the last occurrence of a substring within the string:

    int index=str.LastIndexOf(substring);

    It will give you index of last occurrence.

    Refer details @

    • Edited by Pragati Katiyar Friday, July 5, 2019 4:42 PM
    • Proposed as answer by AR_01 Friday, July 5, 2019 5:18 PM
    Friday, July 5, 2019 4:41 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, July 17, 2019 6:17 AM