Solved

How do i get the required tags inside Panel?

Posted on 2008-11-03
5
379 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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,…
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now