Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2006

Tip: Looking for answers? Try searching our database.

RegEx substring

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bryanmig@gmail.com - 06 Sep 2006 23:27 GMT
Ok I am new to RegEx and what I am trying to do is find a substring.

I have a string that constantly changes.  This string is pulled from an
Atom feed from a blog.  I need to strip the HTML formatting from this
string and just grab the inner text.

If this is my string:  "<div>Hello my name is bryan and I am learning
regex!</div>"

I need to be able to just grab what is in between <div> and </div>

I thought this would work but it still grabs the div's code...

Regex:   <div>.*?</div>

How can i modify this expression to eliminate the div's ?

Thanks
Bryan
Kevin Spencer - 07 Sep 2006 12:16 GMT
(?![^<]*)<[^>]*?>

This matches all HTML markup. It is the opposite of what you want. If you
remove all text matched by this regular expression, what's left over is what
you want.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.

> Ok I am new to RegEx and what I am trying to do is find a substring.
>
[quoted text clipped - 15 lines]
> Thanks
> Bryan
bryanmig@gmail.com - 07 Sep 2006 15:22 GMT
This may not help me, becuase the text I am parsing is code from a blog
and likely to include formatting tags.  I would want to keep all the
formatting markup, whether it be style,s fonts, line breaks, etc.  I
just need to eliminate the first div and last div.

> (?![^<]*)<[^>]*?>
>
[quoted text clipped - 30 lines]
> > Thanks
> > Bryan
Kevin Spencer - 07 Sep 2006 15:55 GMT
Not a problem.

(?<=<div[^>]*>).*?(?=</div>)

I'll explain:

This uses a positive LookBehind and a positive LookAhead. The LookBehind and
LookAhead are non-capturing expressions, which indicate that the Match must
be preceded by or followed by a certain pattern. The Matches in the
LookBehind and LookAhead are not captured. So, only the text between them
is.

In addition, a div may have attributes, so I added an expression to the
LookBehind, indicating that the opening div tag can have any characters in
it other than the '>' character, prior to the closing '>' character.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.

> This may not help me, becuase the text I am parsing is code from a blog
> and likely to include formatting tags.  I would want to keep all the
[quoted text clipped - 36 lines]
>> > Thanks
>> > Bryan
bryanmig@gmail.com - 18 Sep 2006 20:35 GMT
Thanks a million, Kevin

That line of code was golden!
I appreciate your time and effort very much.

Thanks again,
Bryan
http://www.staga.net

---------------------------
> Not a problem.
>
[quoted text clipped - 61 lines]
> >> > Thanks
> >> > Bryan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.