473,549 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ 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="someclas s"> tag. This means the expression should include all other
tags included between <td class="someclas s"> and </td>.

Please help

Regards

Henrik
Dec 7 '05 #1
4 3136
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+c lass=\"myClass\ "\s+>(.*?)</td>");
will give you as result : "Some text"

if you document is like this one : <tr><td class="myClass" > <table><tr><t d
class="anotherC lass">Other text</td></tr></table> </td></tr>
the following regex=new Regex(@"<td\s+c lass=\"myClass\ "\s+>(.*?)</td>");
will give you as result : " <table><tr><t d class="anotherC lass">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="anotherC lass">Other text</td></tr>
that will give you "Some text</td></tr><tr><td class="anotherC lass">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%******** **********@TK2M SFTNGP14.phx.gb l...
Hi

I have a regex question. I want to find all content of a <td
class="someclas s"> tag. This means the expression should include all other
tags included between <td class="someclas s"> 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(s tring content) {
Regex regex=new Regex(@"<td
class=\""somecl ass\"">(?<tdcon tent>.*?((?=<td )|(?=</td))(((?<Open>< td.*?>).*
?((?=<td)|(?=</td)))+((?<Close-Open></td>).*?((?=<td) |(?=</td)))+)*(?(Open )(
?!)))</td>");
MatchCollection matches=regex.M atches(content) ;
foreach(Match match in matches) {
string sMatch=match.Gr oups["tdcontent"].Value;
MessageBox.Show (sMatch);
showTDContent(s Match); //Try to find nested tags
}
}

Here an example :
showTDContent(@ "<table><tr ><td
class=""somecla ss""><table><tr ><td>someText </td><td
class=""somecla ss"">otherTex t</td></tr></table></td></tr><tr><td
class=""somecla ss"">thirdTex t</td></tr></table>");

It returns three strings :
1) <table><tr><td> someText</td><td
class=""somecla ss"">otherTex t</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(s tring content) {
Regex regex=new Regex(@"<td
class=\""somecl ass\"">(?<tdcon tent>.*?((?=<td )|(?=</td))(((?<Open>< td.*?>).*
?((?=<td)|(?=</td)))+((?<Close-Open></td>).*?((?=<td) |(?=</td)))+)*(?(Open )(
?!)))</td>");
MatchCollection matches=regex.M atches(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=\"s omeclass\">(?<t dcontent>
.*?((?=<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%******** **********@TK2M SFTNGP14.phx.gb l... Hi

I have a regex question. I want to find all content of a <td
class="someclas s"> tag. This means the expression should include all other
tags included between <td class="someclas s"> 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
1892
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 a in x by the substring bb and each b by the substring aa. For example, doubleswap(abcab)=bbaacbbaa. Furthermore, let doubleswap(L) be the...
14
3164
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: r'<code>(?P<c>.*?)</code>.*?<targetSeq name="(?P<tn>.*?)">.*?<target>(?P<t>.*?)</target>.*?<align>(?P<a>.*?)</align>.*?<template>(?P<temp>.*?)</template>.*?<an...
4
2193
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 = " s^\?AAA\?01^BBB^g; #Comment "
6
8283
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 string into by defining this variable elsewhere in my module/function/class where I compile the regular expression, thus making for an easy...
4
3212
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no...
2
9849
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, such as change the border color of the textbox to red? That would look quite nice: if the expression fails, the red validator text is shown, plus...
5
477
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 characters, but at the 14th of this character it should fail. Thanks, Todd Meister
2
1658
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 description regular expression - for complitly beginner.
6
1116
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 lksds883 = valid password .. .. ..
7
3801
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way...
0
7484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7755
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. ...
0
7997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6091
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...
0
5124
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3510
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1974
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
1
1089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
798
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.