473,408 Members | 2,734 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,408 software developers and data experts.

Highlighting searching keyword

hiiiiiii
I have a problem in highlighting searching keyword.Actually im using this function for searching
Expand|Select|Wrap|Line Numbers
  1. Public Function HighLight(ByVal Keyword As String, ByVal ContentFor As String)
  2.  
  3.     Dim objHighLight As New highlight(Keyword, "<span class='searchKeyword'>{keyword}</span>")
  4.  
  5.     ContentFor = objHighLight.process(ContentFor, False, False)
  6.     Return ContentFor
  7.     'Dim RegExp As Regex = New Regex(Keyword.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase)
  8.     '' Highlight keywords by calling the MatchEvaluator delegate each time a keyword is found.
  9.     'HighLight = RegExp.Replace(ContentFor, New MatchEvaluator(AddressOf ReplaceKeyWords))
  10.     '' Set the Regex to nothing.
  11.     'RegExp = Nothing
  12. End Function
  13.  
After that i had made a class Highlight.vb in which i defined process function and all procedure of highlighting.
HIGHLIGHT.VB
/start/
Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports Microsoft.VisualBasic
  3. Imports System.Text.RegularExpressions
  4. Imports Common
  5. Public Class highlight
  6. Dim keyword As String
  7. Dim replacement As String
  8. Dim highLightBadTags() As String = New String() {"A", "IMG", "SPAN"}
  9. Dim objCommon As New Common
  10. 'constructor
  11. Sub New(ByVal keyword As String, ByVal replacement As String)
  12.     Me.keyword = keyword
  13.     Me.replacement = replacement
  14. End Sub
  15. Private Function highLighterHTML(ByVal matches As Match) As String
  16.     Dim proceed As String
  17.     'check for bad tags and keyword
  18.     'HttpContext.Current.Response.Write(matches.Groups(0).ToString & " --- " & matches.Groups(1).ToString & " --- " & matches.Groups(2).ToString & " --- " & matches.Groups(3).ToString & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  19.     'HttpContext.Current.Response.Write(matches.Groups(2).ToString & " --- " & matches.Groups(3).ToString & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  20.     If Not Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) Then
  21.     'HttpContext.Current.Response.Write(matches.Groups(2).ToString.ToUpper & " --- " & Me.objCommon.FindInArray(matches.Groups(2).ToString.ToUpper, Me.highLightBadTags) & "<br>")
  22.         If Regex.IsMatch(matches.Groups(0).ToString, "\b(" & Me.keyword & ")\b", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  23.             'put template {keyword} replacement
  24.             proceed = Regex.Replace(matches.Groups(0).ToString, "\b(" & Me.keyword & ")\b", Replace(Me.replacement, "{keyword}", matches.Groups(3).ToString), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  25.         End If
  26.     Else
  27.         proceed = matches.Groups(0).ToString
  28.     End If
  29.     Return proceed
  30. End Function
  31. Private Function highLighterText(ByVal matches As Match) As String
  32.     Dim proceed As String
  33.     'check for bad tags and keyword
  34.     If Not Me.objCommon.FindInArray(matches.Groups(1).ToString.ToUpper, Me.highLightBadTags) Then
  35.         If Regex.IsMatch(matches.Groups(0).ToString, "(" & Me.keyword & ")", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  36.             'put template {keyword} replacement
  37.             proceed = Regex.Replace(matches.Groups(0).ToString, "(" & Me.keyword & ")", Replace(Me.replacement, "{keyword}", matches.Groups(0).ToString), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  38.         Else
  39.             proceed = matches.Groups(0).ToString
  40.         End If
  41.     Else
  42.         proceed = matches.Groups(0).ToString
  43.     End If
  44.  
  45.     Return proceed
  46. End Function
  47. 'main api
  48. Public Function process(ByVal text As String, ByVal keyword As Boolean, ByVal replacement As Boolean)
  49.  
  50.     'if there are specific keyword/replacement given
  51.     If keyword Then
  52.         Me.keyword = keyword
  53.     End If
  54.     If replacement Then
  55.         Me.replacement = replacement
  56.     End If
  57.     If Me.keyword IsNot Nothing AndAlso Me.replacement IsNot Nothing Then
  58.         If Regex.IsMatch(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase) Then
  59.             Return Regex.Replace(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*#si", New MatchEvaluator(AddressOf highLighterHTML), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  60.     Else
  61.         Return Regex.Replace(text, "(" & Me.keyword & ")", New MatchEvaluator(AddressOf highLighterText), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  62.     End If
  63.     'HttpContext.Current.Response.Write(Regex.Replace(text, "(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*si", New MatchEvaluator(AddressOf highLighter), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase))
  64.     'Return Regex.Replace(text, "\b(<([A-Za-z]+)[^>]*[\>]*)*(" & Me.keyword & ")\b(.*?)(<\/\\2>)*si", New MatchEvaluator(AddressOf highLighter), RegexOptions.IgnorePatternWhitespace Or RegexOptions.Singleline Or RegexOptions.IgnoreCase)
  65.     Else
  66.         Return text
  67.     End If
  68. End Function
  69.  
  70. End Class
Now what the problem i got is that i have a string in the text to be search
Expand|Select|Wrap|Line Numbers
  1. <p><img height="161" alt="Polly Gotseva" width="168" align="left" src="/userfiles/polly.jpg" /><b>Polly Gotseva, Managing Director</b></p>
when i search polly keyword it will not show complete image and take string like this
Expand|Select|Wrap|Line Numbers
  1. <p><img height="161" alt="<span class='searchKeyword'>Polly</span> Gotseva" width="168" align="left" src="/userfiles/<span class='searchKeyword'>Polly</span>.jpg" /><b><span class='searchKeyword'>Polly</span> Gotseva, Managing Director</b></p>
now because of image path changed it doesnt show image.That is my problem


if anyone understands my problem please reply soon.Its very urgent
Feb 2 '09 #1
1 2751
Frinavale
9,735 Expert Mod 8TB
You have to make sure that the word you're searching for does not exist as an attribute of a HTML tag before surrounding the word in a <span>.

You have to write a regular expression that checks that makes sure that the word is not replaced if the word for exists after a "<" and before a ">".
Feb 2 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Jay Davis | last post by:
I use xemacs and syntax highlighting for my IDE, but I'm not a big fan of the default syntax highlighting colors. For instance, in 'def fun():' both 'def' and 'fun' are the same color. I kind of...
0
by: Chris Chandler | last post by:
Hello I am developing a web application that uses full text searching quite extensively. This is ideal where I am searching a large number of large varchar fields for a set of key words. My...
1
by: Nic | last post by:
Hi - I am battling to find the a resource, so maybe some one in here can help The problem is as follows: I am trying to build a mod_perl source code editor for the web - to edit Perl source code...
3
by: Antoine Junod | last post by:
Hello, I definitely have a problem to build a clean data structure. I would be very happy if some of you could help me as well as in the past. Here is my problem: -> I have a list of...
4
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax...
7
by: Leif902 | last post by:
After much searching of google, the closest I can find is highlighting search terms... however, this is not what I wanted to do. Does anyone know how to parse through a specific element (lets say...
3
by: Daniel Fetchinson | last post by:
Does Python 2.5.2's embedded SQLite support full text searching? Sqlite itself is not distributed with python. Only a python db api compliant wrapper is part of the python stdlib and as such it...
4
by: CreativeMind | last post by:
hi, i deleted the code from aspx file, not from codebehind. i just typed <script language="javascript"function abc() {return false;}</script>. keywords function and return are not highlighting....
5
Breezwell
by: Breezwell | last post by:
I have done some searching on this one and I have yet to find any information that sheds light on what I am trying to do. At lease from what I can understand. Basically, I have a main form which...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.