473,466 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regular expression help needed

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

Henrik
Dec 7 '05 #1
4 3125
Hi Henrik

I guess something like /^<[^>]+>(.*)<[^>]+>$/ should do the trick,
although I'm not really sure about that greedy matching... (i.e. will
$1 include *everything* after the first tag, or only up to the closing
tag?)
hth,
Markus

Dec 7 '05 #2
It depends on what you really are looking for.

if you document is like this one : <tr><td class="myClass">Some
text</td></tr>
the following regex=new Regex(@"<td\s+class=\"myClass\"\s+>(.*?)</td>");
will give you as result : "Some text"

if you document is like this one : <tr><td class="myClass"> <table><tr><td
class="anotherClass">Other text</td></tr></table> </td></tr>
the following regex=new Regex(@"<td\s+class=\"myClass\"\s+>(.*?)</td>");
will give you as result : " <table><tr><td class="anotherClass">Other text"
wich is not what you are looking for

if you remove the question mark in the regex, it will work for the two
anterior examples but will not work for the following one :
if you document is like this one : <tr><td class="myClass">Some
text</td></tr><tr><td class="anotherClass">Other text</td></tr>
that will give you "Some text</td></tr><tr><td class="anotherClass">Other
text"

My question is : do you have nested <td> tags ? In that case, you have to
use backtracking
Could you give us the most complicated example of file you have ?

Ludovic SOEUR.

"henrik" <no****@thierry.nu> a écrit dans le message de
news:u%******************@TK2MSFTNGP14.phx.gbl...
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

Henrik

Dec 7 '05 #3
I did the case where you can have nested tags, using backtracking :

public void showTDContent(string content) {
Regex regex=new Regex(@"<td
class=\""someclass\"">(?<tdcontent>.*?((?=<td)|(?= </td))(((?<Open><td.*?>).*
?((?=<td)|(?=</td)))+((?<Close-Open></td>).*?((?=<td)|(?=</td)))+)*(?(Open)(
?!)))</td>");
MatchCollection matches=regex.Matches(content);
foreach(Match match in matches) {
string sMatch=match.Groups["tdcontent"].Value;
MessageBox.Show(sMatch);
showTDContent(sMatch); //Try to find nested tags
}
}

Here an example :
showTDContent(@"<table><tr><td
class=""someclass""><table><tr><td>someText</td><td
class=""someclass"">otherText</td></tr></table></td></tr><tr><td
class=""someclass"">thirdText</td></tr></table>");

It returns three strings :
1) <table><tr><td>someText</td><td
class=""someclass"">otherText</td></tr></table>
2) otherText
3) thirdText

If you dont have nested tags like the example before, you can keep the SAME
regex expression but you don't need to use recursivity
public void showTDContent(string content) {
Regex regex=new Regex(@"<td
class=\""someclass\"">(?<tdcontent>.*?((?=<td)|(?= </td))(((?<Open><td.*?>).*
?((?=<td)|(?=</td)))+((?<Close-Open></td>).*?((?=<td)|(?=</td)))+)*(?(Open)(
?!)))</td>");
MatchCollection matches=regex.Matches(content);
foreach(Match match in matches) {
MessageBox.Show(match.Groups["tdcontent"].Value);
}
}

To explain the regular expression, have a look to
http://blogs.msdn.com/bclteam/archiv...15/396452.aspx. It explains
how works balanced matching
<
[^<>]*

(

(

(?<Open><)

[^<>]*

)+

(

(?<Close-Open>>)

[^<>]*

)+

)*

(?(Open)(?!))


My regex is nearly the same:
<td\s+class=\"someclass\">(?<tdcontent>
.*?((?=<td)|(?=</td))
(
(
(?<Open><td.*?>)
.*?((?=<td)|(?=</td))
)+
(
(?<Close-Open></td>)
.*?((?=<td)|(?=</td))
)+
)*
(?(Open)(?!))
)</td>

In fact,
[^<>]* is replaced by .*?((?=<td)|(?=</td)) that means any opening or
closing TD tag
and the other things are exactly the same.
Hope everything helps,

Ludovic SOEUR.
"henrik" <no****@thierry.nu> a écrit dans le message de
news:u%******************@TK2MSFTNGP14.phx.gbl... 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

Henrik

Dec 7 '05 #4
Hi you Guys

Thank you for you help!

I solved it with <td[\ \s]class="myClass">(\s\S]*?)</td>. I donot have
nested tables, so this did the trick. Very close to some of your
suggenstions.

:o)

Regards,

Henrik
Dec 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack Smith | last post by:
I posted this question earlier, but I got no responses. Can anyone help me out here...any hints or even how to start? Thanks in advance. Let doubleswap(x) be the string formed by replacing each...
14
by: Tina Li | last post by:
Hello, I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum recursion limit exceeded". Here is the pattern string: ...
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
6
by: Chris Lasher | last post by:
Hello, I would like to create a set of very similar regular expression. In my initial thought, I'd hoped to create a regular expression with a variable inside of it that I could simply pass a...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
2
by: Dot net work | last post by:
Hello. Say I have a .net textbox that uses a .net regularexpressionvalidator. If the regular expression fails, is it possible to launch a small client side javascript function to do something,...
5
by: tmeister | last post by:
I am in need of a regular expression that tests and fails if there are 14 or more of a character in the test string. There can be up to 13 of these characters in the string and any other...
2
by: PawelR | last post by:
Hello everybody, I have problem with regular expression. I want "code" telephon number and I have two types number: 1) or 2) ( or ) where x - is digital Maybe someone know where is simple...
6
by: Øyvind Isaksen | last post by:
Can anyone please help me make i regular expression (asp.net / vb.net) that check if a password is more than 5 digit/char? Example passwords: whK5v = valid password hd3 = invalid password...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.