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