Need help with regular expression

Posted on 2011-10-23
Last Modified: 2016-09-29
i'm trying to extract the information from an HTML:

                                    <div class="col1"><a href=""></a></div>
                                    <div class="col2">RandomFileName.rar</div>
                                    <div class="col3">277.52 MB</div>
                                    <div class="col4"><span class="icon approved">&nbsp;</span><span class="left">Available</span></div>

Open in new window

what I need is:

File name:
File size:
Available / Not Available.

how could this be done and if so, please explain your answer.
Question by:rotem156
    1 Comment
    LVL 74

    Accepted Solution

    Based on your example, you should be able to use something like:


    Open in new window

    where each datum that you are seeking is in its own named capture group. You can see the names inside the angle brackets above (i.e. filename, filesize, and availability). You would access these group in this manner:

    Match m = Regex.Match(input, pattern);
    string filename = m.Groups["filename"].Value;
    string filesize = m.Groups["filesize"].Value;
    string availability = m.Groups["availability"].Value;

    Open in new window

    If you need an explanation of the pattern, I am glad to accommodate.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
    Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    755 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

    18 Experts available now in Live!

    Get 1:1 Help Now