473,545 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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><contro l 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 5547
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><contro l 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><contro l 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><contro l id=3></control>
<control id=1><control id=2><control id=4></control></control></control><contro l id=3></control>

Sample output:
Matching: <control id=1><control id=2></control></control><contro l 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><contro l 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@Rem oveThisequanimi tyDotnl> 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><contro l 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\"><contr ol id=\"2\"></control></control><contro l id=\"3\"></control>";

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

foreach (XmlNode node in doc.SelectNodes ("/doc/*[count(child::*) >= 1]"))
Console.WriteLi ne(node.OuterXm l);
}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4378
by: John | last post by:
Hi, I was wondering how I would be able to strip the <font> tags from a file when I include it so it can take on the characteristics of my own stylesheet instead of the font tag attributes. Thanks in advance! John
2
2314
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
1
287
by: Picho | last post by:
Hi all, first of all, does anyone know of a newsgroup dedicated for regular expressions? second - my problem: I have a string that represents a formula of the format: "formula(...)" the formula itself can contain other formula expressions like "Calc(...)", "Assign(...)" etc
0
990
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ....<table>...<table>...>Final<...</table>...</table>... I want to match: <table>...>Final<...</table> from this example. The string could also, of course,...
4
3136
by: henrik | last post by:
Hi I have a regex question. I want to find all content of a <td class="someclass"> tag. This means the expression should include all other tags included between <td class="someclass"> and </td>. Please help Regards
17
2766
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted...
1
1959
by: Heron | last post by:
Hi, I'm new to regular expressions and having troubles recreating one that would match tags with there attribute and content. Example on which i'm doing the match: protected void btnLogout_Click(object sender, EventArgs e)<br / {<br /> this._db.SignOut(Session);<br / if (Session != null)<br /> Session.Remove("User");<br / ...
2
4788
by: Sami | last post by:
How can I get the inner content of a tag with regular expression I couldn't the the opening and closing tags to match properly Input "fjkdjfkdj <div>sadfdf dfdf <b>dfd</b>dfdf<div>nested<div>tags</div>.</div> </div>dfdfdf" Get content of the first div tag
0
2766
by: LanaR | last post by:
Hello, one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql. The statement itself is not that complicated, it is 3 selects and union all. Explain output is pretty big, but I could not find anything unusual. I'm...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.