Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Need help with regular expression for nested tags

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ward Bekker - 09 Dec 2005 08:50 GMT
Hi,

I need a regular expression that will match only the tags that have
nested tags inside them:

Input:

<control id=1><control id=2></control></control><control id=3></control>

Goal:

The regexp should match <control id=1><control id=2></control></control>

When i use this regexp: <control\sid=.+?>.+?\</control> it does not
handle the nesting correcly, returning this as a match: <control
id=1><control id=2></control></control>.

Thank you for your help,

Gr. Ward
Ward Bekker - 09 Dec 2005 08:58 GMT
Whoops....last piece should read:

When i use this regexp: <control\sid=.+?>.+?\</control> it does not
handle the nesting correcly, returning this as a match: <control
id=1><control id=2></control>

> Hi,
>
[quoted text clipped - 16 lines]
>
> Gr. Ward
Ken Arway - 10 Dec 2005 03:45 GMT
> Whoops....last piece should read:
>
[quoted text clipped - 22 lines]
>>
>> Gr. Ward

 I took the liberty of assuming you might have nested tags of more than two levels....

Regex regex = new Regex(@"
                (<control\sid=.+?>.+?(?:</control>){2,})",
                (RegexOptions) 0);

Sample input:
<control id=1><control id=2></control></control><control id=3></control>
<control id=1><control id=2><control id=4></control></control></control><control id=3></control>

Sample output:
Matching: <control id=1><control id=2></control></control><control id=3></control>
    1 =»<control id=1><control id=2></control></control>«=

Matching: <control id=1><control id=2><control id=4></control></control></control><control id=3></control>
    1 =»<control id=1><control id=2><control id=4></control></control></control>«=

Signature

Take care,
Ken
(to reply directly, remove the cool car. <sigh>)

Greg Bacon - 14 Dec 2005 21:32 GMT
: I need a regular expression that will match only the tags that have
: nested tags inside them:
[quoted text clipped - 5 lines]
:
: The regexp should match <control id=1><control id=2></control></control>

Have you considered using XPath, which may be a more appropriate tool?

   [STAThread]
   static void Main(string[] args)
   {
     string input = "<control id=\"1\"><control id=\"2\"></control></control><control id=\"3\"></control>";

     XmlDocument doc = new XmlDocument();
     doc.LoadXml("<doc>" + input + "</doc>");

     foreach (XmlNode node in doc.SelectNodes("/doc/*[count(child::*) >= 1]"))
       Console.WriteLine(node.OuterXml);
   }

Hope this helps,
Greg
Signature

I always say there's a three-word lexicon that explains the tax economy:
need, greed, and compassion. Need now means wanting someone else's money,
greed means wanting to keep your own, and compassion is the sentiment of
the politician who wants to arrange the transfer.   -- Joseph Sobran


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.