?
Solved

Regular Expression

Posted on 2006-10-30
5
Medium Priority
?
369 Views
Last Modified: 2010-04-16
Hi ,

I am new to regular expressions and need to extract a part of HTML. Does Carriage Return matter? For example

<div id="subnavmaroon">
<ul>
<li class="picborder"><a href="#">Agriculture</a></li>
<li class="picborder"><a href="#">Mining &amp; Exploration</a></li>
<li><a href="#">Petroleum</a></li>
</ul>
</div>

I require a regular expression which will give me

<li class="picborder"><a href="#">Agriculture</a></li>
<li class="picborder"><a href="#">Mining &amp; Exploration</a></li>
<li><a href="#">Petroleum</a></li>

So the innerHtml of the UL tag

Thanks in advance

Andrew
0
Comment
Question by:REA_ANDREW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17836915
so starting

<div id="subnavmaroon">
<ul>

and ending

</ul>
</div>

anything between I need to return
0
 
LVL 6

Accepted Solution

by:
der_jth earned 2000 total points
ID: 17837114
Match m = Regex.Match(input, @"<div id=""subnavmaroon"">\s*<ul>(.*?)</ul>\s*</div>", RegexOptions.Singleline);
string result = m.Groups[1].Value;

Let me know if you have any issues with this.
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17837256
thank you for getting back to me.  I need to replace what it finds with nothing.  It is searching a file so it will be over multiple lines.

Thanks

Andrew
0
 
LVL 6

Expert Comment

by:der_jth
ID: 17837317
Yeah, just a small adaptation then:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {

      string input = @"
This is some text to be preserved
<div id=""subnavmaroon"">
<ul>
<li class=""picborder""><a href=""#"">Agriculture</a></li>
<li class=""picborder""><a href=""#"">Mining &amp; Exploration</a></li>
<li><a href=""#"">Petroleum</a></li>
</ul>
</div>
This was after the emptied div";

      string result = Regex.Replace(
        input,
        @"<div id=""subnavmaroon"">\s*<ul>(.*?)</ul>\s*</div>",
        @"<div id=""subnavmaroon""></ul></div>",
        RegexOptions.Singleline
      );

      Console.WriteLine(result);
      Console.ReadLine();
    }
  }
}

--

Outputs:

--
This is some text to be preserved
<div id="subnavmaroon"></ul></div>
This was after the emptied div
0
 
LVL 6

Expert Comment

by:der_jth
ID: 17837326
Oops... You'll probably want to throw in a <ul> start tag into the string that is the third argument to Regex.Replace. Of course, if you want to wipe the div as well, just make the third argument an empty string.
0

Featured Post

Independent Software Vendors: 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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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