473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 5541
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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. ...
2
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...
1
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...
0
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: ...
4
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>....
17
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...
1
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...
2
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...
0
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.