473,385 Members | 1,384 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,385 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 4781
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.