472,973 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

get inner content with regular expression

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

Output
"sadfdf dfdf <b>dfd</b>dfdf<div>nested<div>tags</div>.</div"
thank you
Sami

Aug 24 '08 #1
2 4761
On Aug 24, 3:17*pm, "Sami" <sam...@ymail.comwrote:
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

Output
"sadfdf dfdf <b>dfd</b>dfdf<div>nested<div>tags</div>.</div"

thank you
Sami
There is no way to do it using regular expressions other than
hardcoding it as there are numerous <divand </divtags in the main
<divtag. In the program I'm building right now, I use regex to find
the content between two tags in html (if it were xml it would be much
easier!), but i don't have multiple tags with the same name.
Now, if your content is xml (it can be html but it must be well-
formed), there is a much easier approach. You just read it as an xml
document and you search for the correct tag node. Very simple. (To see
if your html fits, google well-formed html checker).
Aug 25 '08 #2
Hello maximz2005,
On Aug 24, 3:17 pm, "Sami" <sam...@ymail.comwrote:
>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

Output
"sadfdf dfdf <b>dfd</b>dfdf<div>nested<div>tags</div>.</div"
thank you
Sami
There is no way to do it using regular expressions other than
hardcoding it as there are numerous <divand </divtags in the main
<divtag. In the program I'm building right now, I use regex to find
the content between two tags in html (if it were xml it would be much
easier!), but i don't have multiple tags with the same name.
Now, if your content is xml (it can be html but it must be well-
formed), there is a much easier approach. You just read it as an xml
document and you search for the correct tag node. Very simple. (To see
if your html fits, google well-formed html checker).
You can use the HTMLAgility pack (on codeplex) to rea the HTML as it were
XML and you could easily get the contents you wanted. You can also use regex
for this, though you'd end up the the more advanced constructs (the hardest
to understand ones) like the balanced group sets. (more info here: http://blogs.msdn.com/bclteam/archiv...15/396452.aspx)

--
Jesse Houwing
jesse.houwing at sogeti.nl
Aug 25 '08 #3

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

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: Bernard A. | last post by:
hello, while trying to play with generator, i was looking for an idea to get the position of a inner list inside another one, here is my first idea : - first find position of first inner...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
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...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
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...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
3
Spippo
by: Spippo | last post by:
Hello Is it possible to retrieve then inner-text from a link with regular expressions? For example, I can retrieve all links with regular expressions. With: string pattern = @"<a...
9
by: netimen | last post by:
I have a text containing brackets (or what is the correct term for '>'?). I'd like to match text in the uppermost level of brackets. So, I have sth like: 'aaaa 123 < 1 aaa < t bbb < a <tt ff 2...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.