?
Solved

Need help with regular expression

Posted on 2011-10-23
1
Medium Priority
?
386 Views
Last Modified: 2016-09-29
i'm trying to extract the information from an HTML:

<ul>
                    			                                <li>
                                    <div class="col1"><a href="">http://filejungle.com/f/JHYjbd/RandomFileName.rar</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>
                                </li>
                                                                
                                                                
                            </ul>

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.
0
Comment
Question by:rotem156
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37015796
Based on your example, you should be able to use something like:

<div\s+class="col2">(?<filename>[^<]+)</div>\s*<div\s+class="col3">(?<filesize>[^<]+)</div>\s*<div\s+class="col4"><span[^>]*>[^<]*</span><span[^>]*>(?<availability>[^<]+)

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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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…
Suggested Courses

862 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