Strip first comment

Posted on 2007-07-26
Last Modified: 2010-04-15
I have this

<!-- Generated by abcdefg --><p>sdsdsd</p>

I need to remove the first comment that is <!-- Generated by abcdefg --> and leave the rest intact.
Hopefully, I need to use regex.
Question by:ansi_c
    LVL 22

    Expert Comment

    if abcdefg is always the same, you can just use:

    string removed = data.Replace("<!-- Generated by abcdefg -->", String.Empty);
    LVL 53

    Expert Comment

    Without regex:

            Dim myString As String = "<!-- Generated by abcdefg --><p>sdsdsd</p>"
            myString = myString.Substring(myString.IndexOf("-->") + 3)

    Author Comment

    No. It's always changing.
    And I want a regular expression solution.
    LVL 22

    Accepted Solution

    Very well.

    data = Regex.Replace(data, "^<!-- Generated by .+? -->", String.Empty);
    LVL 1

    Expert Comment


    you might want to limit that a bit, if you only have "A-Z" in the list with no spaces

    data = Regex.Replace(data, @"^<!-- Generated by \w* -->", String.Empty);

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem with C# 1 44 mvc 2 32
    Build VS2015 project in bat command 3 55
    .xltm file opens as .xlsx file 3 24
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now