Solved

How do i get the required tags inside Panel?

Posted on 2008-11-03
5
384 Views
Last Modified: 2012-05-05
I have a regular expression:

  string st = @"<asp:Panel ID='pnlDockWrapper1' runat='server' style='border:solid 1px red;margin-bottom:5px;'>
    <div id='pnlDescription1' runat='server' title='WidgetTitle'>
    </div>
    <asp:PlaceHolder ID='pnlImage1' runat='server'></asp:PlaceHolder>
    <div id='pnlDescription2' runat='server'>
    </div>
</asp:Panel>
<asp:Panel ID='pnlDockWrapper30' runat='server' style='border:solid 1px blue;margin-bottom:5px;'>
    <div id='pnlDescription30' runat='server' title='WidgetTitle'>
    </div>
    <asp:PlaceHolder ID='pnlImage30' runat='server'></asp:PlaceHolder>
    <asp:PlaceHolder id='pnlImage31' runat='server'>
    </asp:PlaceHolder>
</asp:Panel>
<asp:Panel ID='pnlDockWrapper60' runat='server' style='border:solid 1px green;margin-bottom:5px;'>
    <div id='pnlDescription60' runat='server' title='WidgetTitle'>
    </div>
    <div ID='pnlDescription61' runat='server'></div>
    <div id='pnlDescription62' runat='server'>
    </div>
</asp:Panel>";

 Regex myRegexTexts = new Regex(@"\w*pnlDockWrapper[\w*\s*\W*]*(?<Texts>pnlDescription\d+)[\w*\s*\W*]*</asp:Panel>", RegexOptions.IgnoreCase);

 MatchCollection collTexts = myRegexTexts.Matches(st);

What i'm trying to do is to read all divs with id "pnlDescription\d+" within the panel with id=pnlDockWrapper.

What should be the right regular expression??
0
Comment
Question by:nestenius
[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
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 22870068
I'm going to bed now, but you really want to have a look at http://www.regexbuddy.com

it is great for inserting text and applying and testing regular rexpressions for almost all languages. Goodluck!
0
 
LVL 4

Accepted Solution

by:
SyfAldeen earned 500 total points
ID: 22884809
nestenius,
Best Solution:
Use the first RegEx to extract each pnlDockWrapper. This will give three results:
  1. <asp:Panel ID='pnlDockWrapper1' runat='server' style='border:solid 1px red;margin-bottom:5px;'>
        <div id='pnlDescription1' runat='server' title='WidgetTitle'>
        </div>
        <asp:PlaceHolder ID='pnlImage1' runat='server'></asp:PlaceHolder>
        <div id='pnlDescription2' runat='server'>
        </div>
    </asp:Panel>
  2. <asp:Panel ID='pnlDockWrapper30' runat='server' style='border:solid 1px blue;margin-bottom:5px;'>
        <div id='pnlDescription30' runat='server' title='WidgetTitle'>
        </div>
        <asp:PlaceHolder ID='pnlImage30' runat='server'></asp:PlaceHolder>
        <asp:PlaceHolder id='pnlImage31' runat='server'>
        </asp:PlaceHolder>
    </asp:Panel>
  3. <asp:Panel ID='pnlDockWrapper60' runat='server' style='border:solid 1px green;margin-bottom:5px;'>
        <div id='pnlDescription60' runat='server' title='WidgetTitle'>
        </div>
        <div ID='pnlDescription61' runat='server'></div>
        <div id='pnlDescription62' runat='server'>
        </div>
    </asp:Panel>
Then, make a loop to apply the second RegEx to each result of the first RegEx. This will give six (two + one + three) results:
  1. <div id='pnlDescription1' runat='server' title='WidgetTitle'>\r\n    </div>
      Texts=pnlDescription1
  2. <div id='pnlDescription2' runat='server'>\r\n    </div>
      Texts=pnlDescription2
  3. <div id='pnlDescription30' runat='server' title='WidgetTitle'>\r\n    </div>
      Texts=pnlDescription30
  4. <div id='pnlDescription60' runat='server' title='WidgetTitle'>\r\n    </div>
      Texts=pnlDescription60
  5. <div ID='pnlDescription61' runat='server'></div>
      Texts=pnlDescription61
  6. <div id='pnlDescription62' runat='server'>\r\n    </div>
      Texts=pnlDescription62
     
Alternatives:
  1. You can use the third RegEx -corrected version of your original RegEx- only instead of the first and the second without the need to loop if each pnlDockWrapper panel contains only one pnlDescription.
  2. You can use the second RegEx only without the need to loop if you are sure that only pnlDockWrapper panels exist in the file.
<asp:Panel ID='pnlDockWrapper[\w\W]*?</asp:Panel>
<div id='(?<Texts>pnlDescription\d+)[\w\W]*?<\/div>
<asp:Panel ID='pnlDockWrapper[\w\W]*?<div id='(?<Texts>pnlDescription\d+)[\w\W]*?<\/div>[\w\W]*?</asp:Panel>

Open in new window

0
 
LVL 1

Author Comment

by:nestenius
ID: 22893595
Hi svfaldeen,
Thanx for the comment.

Your reply's quite good, however, i needed to modify the regex a bit. Just to check, in case, there's space in between.


<asp:Panel\s*ID\s*=\s*[",']pnlDockWrapper[\w\W\s]*?</asp:Panel>
<div\s*id\s*=\'(?<Texts>pnlDescription\d+)[\w\W]*?<\/div>

Open in new window

0
 
LVL 1

Author Closing Comment

by:nestenius
ID: 31512633
Nice to know you.
0
 
LVL 4

Expert Comment

by:SyfAldeen
ID: 22893812
nestenius,
Using \s in [\w\W\s] is useless. [\w\W] means 'anything'... a word or non-word char.
Using , in [",'] is wrong if you intend to use it as separator. This will cause the RegEx to accept any of the three chars (Double Quote, Comma, Single Quote)!
I think that you are using .Net, therefore, use ' directly not \'.
Finally, you missed one more \s* in the second RegEx after = ;-).
Here you are the refined RegEx:

<asp:panel\s*id\s*=\s*["']pnlDockWrapper\d+["'][\w\W]*?</asp:panel>
<div\s*id\s*=\s*["'](?<Texts>pnlDescription\d+)["'][\w\W]*?</div>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 102
orderby list (from Json) 1 44
REGEXEXTRACT in google sheets 2 33
C# LINQ 5 43
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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…

739 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