[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

How do i get the required tags inside Panel?

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
nestenius
Asked:
nestenius
  • 2
  • 2
1 Solution
 
Xyptilon2Commented:
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
 
SyfAldeenCommented:
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
 
nesteniusAuthor Commented:
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
 
nesteniusAuthor Commented:
Nice to know you.
0
 
SyfAldeenCommented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now