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??
LVL 1
nesteniusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.