Need help with regular expression for nested tags

W

Ward Bekker

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
 
W

Ward Bekker

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>
 
K

Ken Arway

Ward said:
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>

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>«=
 
G

Greg Bacon

: 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>

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top