?
Solved

Regular Expression

Posted on 2006-10-30
5
Medium Priority
?
371 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
  • 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

839 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