Results 1 to 7 of 7

Thread: [RESOLVED] Replace text that's similar to text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    79

    Resolved [RESOLVED] Replace text that's similar to text

    Hi. I have a long string that I'm retrieving from a file. I have a list in the database of different strings. I want to take my list and compare it to the strings from the file. For all instances that it finds I want it to format the text as a link.
    My issue is that I want to find the text that starts with specific letters and ends with a specific letter and then turn that part of the strng into an href. I need s/t like the percent symbol in SQL.
    What I want to say is
    Replace(string that has the text to replace, T%t. "<a href= "T%t."> T%t"
    The text I want to search for is the word "Text."

    Thanks.

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Replace text that's similar to text

    You will likely need to handle each string from the database separately. I would suggest creating a function which takes the source string, starting and ending delimiters, target string, and a flag which indicates whether you want to convert just the first or all instances. I suggest building a module and placing this code in a module. This promotes code reuse (you can then insert the module into future programs and you have these subroutines and functions already coded).

    To find and replace the target string you can use two different approaches. The first is to use the string function IndexOf to find the first instance of the start delimiter, and then the first instance of the end delimiter after that point. Extract the text with a Substring command and compare with the target string. Alternatively, use the string Split command, splitting on the start delimiter. Note this is a destructive process - the start delimiter is thrown away in this process. It has the benefit of breaking up the source string into an array with each element starting with the text between the delimiters. You still need to check for the end delimiter to figure out the value of the bounded text. Compare this with the target string and replace the text as necessary. Using the split approach you need to reconstitute the final modified string by concatenating the array elements together again.

  3. #3
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Replace text that's similar to text

    I was wondering if Regular Expressions would help you. I've not done anything with these before, but I put this application together to see if it would work. Hope it gives you some ideas at least...

    The idea is you type a string into TextBox1, hit the button and the processed text appears in TextBox2. I've coded it so that text starting with BEGIN and ending with END is replaced by the same text but strarting with START and ending with FINISH.

    As John SC points out, you would have to handle each string from the database separately.

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = " rae; nj;anvr;e agnre;ag[BEGINa egreangrlaeg] ruENDBEGINea grea ;grea[]gre ENDlreaugilreag[ reaguirela BEGINea ] erluaig rea[ grea] greEND re"
            TextBox2.Clear()
    
            Dim str As String = TextBox1.Text
    
            Dim strProcess As String
    
            Do
                strProcess = Regex.Match(str, "BEGIN.+?END").ToString
                If strProcess.Length > 0 Then
                    Dim strNew As String = strProcess.Replace("BEGIN", "START").Replace("END", "FINISH")
                    str = str.Replace(strProcess, strNew)
                End If
            Loop Until strProcess = ""
    
            TextBox2.Text = str
    
        End Sub
    End Class
    This world is not my home. I'm just passing through.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Replace text that's similar to text

    If you haven't used RegEx, then you have two options: Learn it for this, or go about it the way JohnSC was suggesting. RegEx will find those strings pretty well, but it is kind of an arcane language in itself. There are some good tutorials on it, and good references...which, unfortunately, I have on a different computer, but you can Google on that and find someting.
    My usual boring signature: Nothing

  5. #5
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Replace text that's similar to text

    You have to be careful with the approach from Trisuglow. Although regular expressions can be very useful, you have to be careful with global use of Replace. If there are any single BEGIN or END they will also get replaced. Even thing like BEGINNING would be changed to STARTNING and BLEND would be changed to BLFINISH.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    79

    Re: Replace text that's similar to text

    Thank you everyone for your help.
    I guess I can use the IndexOf in a loop. Find the first/second/etc instance of the start and then the end delimiter after that instance of the start delimiter. Then I can use the substring to get that and replace it as an href.
    Regular expressions sound interesting as well and are probably more efficient. But, I don't want every instance of just the end delimiter to be replaced. I only want it to be replaced if it finds the start and the end together. Can that work with regular expressions?

    Thanks.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    79

    Re: Replace text that's similar to text

    In the end I used a regular expression to find the beginning and the end characters (with characters in between). Then I looped through the matches and for every match that it found I replaced that match with the text that I wanted it to replace. It's working nicely.
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width