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 / May 2004

Tip: Looking for answers? Try searching our database.

Help with highlighting text using RegEx

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Samuel Hon - 10 May 2004 22:15 GMT
Hi

I've been tearing my hair out with this problem, I'm not particuarly
good with more complex reg expressions. Basically, I'm trying to
highlight search keywords.

For example:

http://www.google.co.uk/search?q=regular+expressions

If the phrase regular expressions appears, then the text is bold. This
I can achieve.

BUT

and there had to be one, if a word appears in a URL, then I need to
ignore it. One of the results is www.regular-expressions.info (which I
have looked at). Using the simple expression will change the url to

www.<B>regular</B>-<B>expressions</B>.info

which obviously is bad.

Any help would be appreciated

Thanks

Sam
Niki Estner - 10 May 2004 22:37 GMT
I think you need a negative lookbehind, something like:

"(?<!www\.\S*)regular"

This will match the word "regular" if it's not prefixed by a "www." followed
by any number of non-whitespace characters. (not sure if this is enough, but
you should be able to fine-tune it)
In general a negative lookbehind "(?<!...)" will prevent a match if the
"..." part can be matched left of the "real" match.

Hope this helps

Niki

> Hi
>
[quoted text clipped - 24 lines]
>
> Sam
Samuel Hon - 12 May 2004 09:49 GMT
Thanks Niki

Unfortunately I cant search for http:// or www because the search
value could be in the querystring or in the page name. I've been
trying to look for href= and then find the closing >

I'll have a fiddle with your suggestion.

Thanks again

> I think you need a negative lookbehind, something like:
>
[quoted text clipped - 38 lines]
> >
> > Sam
Samuel Hon - 13 May 2004 16:40 GMT
I have a slightly different approach now which is going wrong because
the MatchCollection has 8 values (correct) which are blank (incorrect)

Can anyone see what i'm blatantly missing?

Ta in advance

Dim strText As String = "This is my long string with a <a
href=""http://localhost/test.aspx"">Test Link using quotes</a>,<BR>a
<a href = 'http://localhost/test.aspx'>Test Link using apostrophes</a>
<BR> and one with a <A
HREF=""http://localhost/test.aspx?test"">querystring</a>."

public Function Highlight(strText As String)
   
    Dim objRegEx As New RegEx("<[^<>]*>", RegexOptions.IgnoreCase)

       'Find All tags and place in collection
    Dim colM As MatchCollection = objRegEx.Matches(strText)

       'Replace all tags with $!$
    Dim strReplaced As String = objRegEx.Replace(strText,"$!$")
   
       'Create new objRegEx looking for test
    objRegEx = New RegEx("(test)", RegexOptions.IgnoreCase)
   
       'Replace All with highlight
    strText = objRegEx.Replace(strReplaced, _
                "<span style='background-color:#FFFF66;'>$1</span>")

       'Loop through all items in the collection replacing $!$
    For i As Integer = 0 to colM.Count -1
        Dim m As Match = colM.Item(i)
        strText = strText.Replace("$!$",m.ToString()) & vbcrlf
    Next i

       'Return
    Return strText
   
End Function
Samuel Hon - 13 May 2004 16:51 GMT
Ignore my last message, I was being stupid and forgot what Replace does

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.