Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highligth word and entities problem

Status
Not open for further replies.

sonya9879

Programmer
Jun 18, 2004
147
0
0
CA
hi, I have written a highlight function based on an array of words. Apparently was working fine until I added entities. Could someone suggest an alternative way to this problem?

>>highlight(list_of_words, text)

Function Highlight(ByVal searchStr As String, _
ByVal inputTxt As String) As String
Dim regexp As Regex
Dim intSearchLoopCounter As Integer
Dim arrySearchWord As String() 'Array to hold the words to be searched for
'Split each word to be searched up and place in an array
arrySearchWord = Split(Trim(searchStr), " ")
regexp = New Regex("", RegexOptions.IgnoreCase)
'Loop round to search for each word to be searched
For intSearchLoopCounter = 0 To UBound(arrySearchWord)
InputTxt = Regex.Replace(InputTxt, "\b(" & Regex.Escape(arrySearchWord(intSearchLoopCounter)) & ")\b", _
New MatchEvaluator(AddressOf ReplaceKeyWords), RegexOptions.IgnoreCase)
Next
Return InputTxt
End Function

Public Function ReplaceKeyWords(ByVal m As Match) As String
Return "<span class=highlight>" & m.Value & "</span>"
End Function


Sample Text: this is a sample &quot;test&quot and I just want to highlight the words inside the array and nothing else

list_of_words: inside, array

thanks a lot
 
thanks god i found an alternative way that it works so I though to share it with you all :)

Function Highlight(ByVal searchStr As String, _
ByVal inputTxt As String) As String

' Setup the regular expression and add the Or operator.
Dim RegExp As Regex = New Regex(Trim(Search_Str).Replace(" ", "|").Trim(), RegexOptions.IgnoreCase)

If Search_Str = "" Then
' display as it is meaning InputTxt=InputTxt
Else
' Highlight keywords by calling the MatchEvaluator delegate each time a keyword is found.
InputTxt = RegExp.Replace(InputTxt, New MatchEvaluator(AddressOf ReplaceKeyWords))
End If

' Set the Regex to nothing.
RegExp = Nothing

Return InputTxt
End Function

 
well, before u posted i was working on it, thanks, i figured some stuff out too...

this works below, but i am going to try what u posted above too...
Code:
<%@ Page Language="VB" Src="test.aspx.vb" Inherits="test" %>

<html>
<head runat="server">
    <title>Highlighting Page</title>
    <link rel="stylesheet" type="text/css" media="screen" href="css\page.css">
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtSearch" runat="server" OnTextChanged="start" AutoPostBack="true"></asp:TextBox><br />
            <asp:Label ID="searchPhrase" runat="server" 
            Text="hi, I have written a highlight function based on an array of words. Apparently was working fine until I added entities. Could someone suggest an alternative way to this problem?"></asp:Label><br />
            <br />
            <asp:Label ID="searchedWords" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>
Code:
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Imports System.Web.Security
Imports Microsoft.VisualBasic

Public Class test
    Inherits System.Web.UI.Page

    Protected searchedWords, searchPhrase As Label
    Protected txtSearch As TextBox

    Sub start(ByVal sender As Object, ByVal e As EventArgs)
        Dim item As Object
        Dim searchResult As String = searchPhrase.Text
        Dim StartTag As String = "<span class=highlight>"
        Dim EndTag As String = "</span>"
        Dim test As Array = Split(Trim(txtSearch.Text), " ")
        For Each item In test
            searchResult = Regex.Replace(searchResult, "\b(" & Regex.Escape(item.ToString()) & ")\b", StartTag & "$1" & EndTag, RegexOptions.IgnoreCase)
        Next
        searchPhrase.Text = searchResult
    End Sub

End Class
 
hey adamroof you are a star thanks for trying to help. the problem I originally had on my code was that I was doing the replacement and adding the tag to every single part of the string instead of JUST replacing the word I wanted when it was found on the list, with the <span> tag. Now I just call RegExp when I have a word to search for. Thanks a lot anyway for your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top