471,325 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Need help with regular expression for nested tags

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
Dec 9 '05 #1
3 5442
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>

Ward Bekker wrote:
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

Dec 9 '05 #2
Ward Bekker wrote:
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>

Ward Bekker wrote:
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


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>=
--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Dec 10 '05 #3
In article <#I**************@TK2MSFTNGP10.phx.gbl>,
Ward Bekker <wDotbekker@RemoveThisequanimityDotnl> wrote:

: 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
--
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
Dec 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by John | last post: by
2 posts views Thread by Joh | last post: by
1 post views Thread by Picho | last post: by
reply views Thread by Greg Vereschagin | last post: by
4 posts views Thread by henrik | last post: by
17 posts views Thread by Mark | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.