Results 1 to 11 of 11

Thread: Replacing Strings based on Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Exclamation Replacing Strings based on Text File

    Hello guys! I USE VISUAL BASIC 2008!

    The problem is that I have a plain text file in which TAG's have that the user can customize. So far so good. Everything is saved in text file, like this:
    example:


    Code:
    <tag1> = http://www.vbforums.com/
    <tag2> = http://www.google.com.br
    <tag3> = http://www.youtube.com.br
    Anyway, it's a huge list.

    There's a RichTextBox in this Form. I want that when user writes some of Tags (for example <tag1>), the same be replaced by what is after the equal sign (in the case, http://www.vbforums.com/).

    EXAMPLE:
    Code:
    Hi! I'm fan of <tag1>
    BECAME:
    Code:
    Hi! I'm fan of http://www.vbforums.com/
    Is it possible?
    Grateful, RickHB.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Re: Replacing Strings based on Text File

    Does someone can help me?

    Please!

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replacing Strings based on Text File

    Yes it's possible but with varying degrees of difficulty. Do you want the tags to be replaced instantly or is it enough to do it as a finalising process for the whole text? And now huge is huge?

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Re: Replacing Strings based on Text File

    Quote Originally Posted by dunfiddlin View Post
    Yes it's possible but with varying degrees of difficulty. Do you want the tags to be replaced instantly or is it enough to do it as a finalising process for the whole text? And now huge is huge?
    One need not be replaced as text is typed. It is when you press a button to finalize the text.

    I hope you understood what I wanted to do.
    These tags can be changed as the user wishes.

    An yeah, the list is Huge... There're 60 Tag's or more, the user can choose.
    Last edited by RickHB; Aug 9th, 2012 at 07:33 AM.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replacing Strings based on Text File

    Ok then. First 60+ is not huge. I use a similar list of 19000+ in one of my programs! So, next question, how are you intending to let the user know what tag represents what site? Will there be a permanent visible key of some sort?

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Re: Replacing Strings based on Text File

    This is not the problem. The user can ADD or REMOVE Tag's acordding his need.

    I just need that the richtextbox replace what is before the "=" by the string after "=", but these TAG's can vary, the user can change it. Everything is save in a text file in that structure that I showed.

  7. #7
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Replacing Strings based on Text File

    You can try something like:

    vb Code:
    1. 'Replaces tags in text and returns the result.
    2.     Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String
    3.  
    4.         Dim parsedtext As New StringBuilder
    5.         Dim indexst, indexgt, curindex As Integer
    6.         Dim tag As String
    7.  
    8.         'Reset current progress and get the location of first "<"c
    9.         curindex = 0
    10.         indexst = text.IndexOf("<"c)
    11.  
    12.         While indexst > 0
    13.  
    14.             'Append the text before the "<"c to the result.
    15.             parsedtext.Append(text.Substring(curindex, indexst - curindex))
    16.  
    17.             'Find the first ">"c after the found "<"c
    18.             indexgt = text.IndexOf(">"c, indexst)
    19.  
    20.             'If no ">"c is found, we're done.
    21.             If indexgt < 0 Then
    22.  
    23.                 'Append the rest of the text to the result
    24.                 parsedtext.Append(text.Substring(indexst))
    25.  
    26.                 Return parsedtext.ToString
    27.  
    28.             End If
    29.  
    30.             'Retrieve the text between "<"c and ">"c
    31.             tag = text.Substring(indexst + 1, indexgt - indexst - 1)
    32.  
    33.             'Is the tag in the dictionary?
    34.             If dict.ContainsKey(tag) Then
    35.  
    36.                 'Append the text associated with the tag to the result.
    37.                 parsedtext.Append(dict(tag))
    38.  
    39.             Else
    40.  
    41.                 'Append <tag> to the result
    42.                 parsedtext.Append(text.Substring(indexst, indexgt - indexst + 1))
    43.  
    44.             End If
    45.  
    46.             'Update the current progress within the string
    47.             curindex = indexgt + 1
    48.  
    49.             'Find index of "<"c after the current index
    50.             indexst = text.IndexOf("<"c, curindex)
    51.  
    52.         End While
    53.  
    54.         'Append the last bit of text.
    55.         parsedtext.Append(text.Substring(curindex))
    56.  
    57.         Return parsedtext.ToString
    58.  
    59.     End Function
    60.  
    61.     'Reads tags and descriptions from file and organizes them into a dictionary ordered by the tag.
    62.     Private Function readtags(ByVal filename As String) As Dictionary(Of String, String)
    63.  
    64.         Static parse As Regex = New Regex("^\<(?<tag>[^\>]+)\>\s*=\s*(?<desc>.+)$", RegexOptions.Compiled)
    65.  
    66.         Dim lines() As String = File.ReadAllLines(filename)
    67.         Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
    68.         Dim m As Match
    69.  
    70.         For Each line As String In lines
    71.  
    72.             'Try to validate the line.
    73.             m = parse.Match(line)
    74.  
    75.             'Was line valid?
    76.             If m.Success Then
    77.  
    78.                 'Get tag and description
    79.                 Dim tag As String = m.Groups(1).Captures(0).Value
    80.                 Dim desc As String = m.Groups(2).Captures(0).Value
    81.  
    82.                 'Add the entry to the dictionary, unless an entry with tht tag is already present.
    83.                 If Not dict.ContainsKey(tag) Then dict.Add(tag, desc)
    84.  
    85.             End If
    86.  
    87.         Next
    88.  
    89.         Return dict
    90.  
    91.     End Function

    Usage:
    Code:
    TextBox1.Text = replacetags("I love '<tag1>'.", readtags(mytagfile))
    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replacing Strings based on Text File

    vb.net Code:
    1. Private Sub Submit_Click(sender As System.Object, e As System.EventArgs) Handles Submit.Click
    2.         'read the file
    3.         Dim lines() As String = System.IO.File.ReadAllLines("tagfile.txt")
    4.         'set the text to a single line
    5.         With RichTextBox1
    6.             .WordWrap = False
    7.             .Multiline = False
    8.         End With
    9.        
    10.         For Each line In lines 'check all tags
    11.             Dim s() As String = Split(line, " = ") 's(0) = "<tag>", s(1) = "replacement text"
    12.  
    13.             'replace all instances of current tag
    14.             If RichTextBox1.Text.Contains(s(0)) Then
    15.                 RichTextBox1.Text = RichTextBox1.Text.Replace(s(0), s(1))
    16.             End If
    17.         Next
    18.         'reset text box
    19.         With RichTextBox1
    20.             .WordWrap = True
    21.             .Multiline = True
    22.         End With
    23.  
    24.         'whatever else you want to do
    25.     End Sub
    Last edited by dunfiddlin; Aug 10th, 2012 at 11:46 AM. Reason: simplified code after change of concept

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replacing Strings based on Text File

    Quote Originally Posted by ThomasJohnsen View Post
    You can try something like:

    vb Code:
    1. 'Replaces tags in text and returns the result.
    2.     Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String
    Not really a big enough collection to justify a dictionary, IMHO.

  10. #10
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Replacing Strings based on Text File

    Quote Originally Posted by dunfiddlin View Post
    Not really a big enough collection to justify a dictionary, IMHO.
    You're probably right. I opted for readability and speed.
    (scanning the entire text for each tag vs. scanning once).

    #EDIT: It is worth noting though, that the number of tags isn't the only determining factor. Your algorithm's is basically O(mn) whereas mine is O(m) + O(n) {m being the number of tags and n the length of the input string}.
    Last edited by ThomasJohnsen; Aug 10th, 2012 at 01:37 PM.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Thumbs up Re: Replacing Strings based on Text File

    Quote Originally Posted by ThomasJohnsen View Post
    You can try something like:

    vb Code:
    1. 'Replaces tags in text and returns the result.
    2.     Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String
    3.  
    4.         Dim parsedtext As New StringBuilder
    5.         Dim indexst, indexgt, curindex As Integer
    6.         Dim tag As String
    7.  
    8.         'Reset current progress and get the location of first "<"c
    9.         curindex = 0
    10.         indexst = text.IndexOf("<"c)
    11.  
    12.         While indexst > 0
    13.  
    14.             'Append the text before the "<"c to the result.
    15.             parsedtext.Append(text.Substring(curindex, indexst - curindex))
    16.  
    17.             'Find the first ">"c after the found "<"c
    18.             indexgt = text.IndexOf(">"c, indexst)
    19.  
    20.             'If no ">"c is found, we're done.
    21.             If indexgt < 0 Then
    22.  
    23.                 'Append the rest of the text to the result
    24.                 parsedtext.Append(text.Substring(indexst))
    25.  
    26.                 Return parsedtext.ToString
    27.  
    28.             End If
    29.  
    30.             'Retrieve the text between "<"c and ">"c
    31.             tag = text.Substring(indexst + 1, indexgt - indexst - 1)
    32.  
    33.             'Is the tag in the dictionary?
    34.             If dict.ContainsKey(tag) Then
    35.  
    36.                 'Append the text associated with the tag to the result.
    37.                 parsedtext.Append(dict(tag))
    38.  
    39.             Else
    40.  
    41.                 'Append <tag> to the result
    42.                 parsedtext.Append(text.Substring(indexst, indexgt - indexst + 1))
    43.  
    44.             End If
    45.  
    46.             'Update the current progress within the string
    47.             curindex = indexgt + 1
    48.  
    49.             'Find index of "<"c after the current index
    50.             indexst = text.IndexOf("<"c, curindex)
    51.  
    52.         End While
    53.  
    54.         'Append the last bit of text.
    55.         parsedtext.Append(text.Substring(curindex))
    56.  
    57.         Return parsedtext.ToString
    58.  
    59.     End Function
    60.  
    61.     'Reads tags and descriptions from file and organizes them into a dictionary ordered by the tag.
    62.     Private Function readtags(ByVal filename As String) As Dictionary(Of String, String)
    63.  
    64.         Static parse As Regex = New Regex("^\<(?<tag>[^\>]+)\>\s*=\s*(?<desc>.+)$", RegexOptions.Compiled)
    65.  
    66.         Dim lines() As String = File.ReadAllLines(filename)
    67.         Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
    68.         Dim m As Match
    69.  
    70.         For Each line As String In lines
    71.  
    72.             'Try to validate the line.
    73.             m = parse.Match(line)
    74.  
    75.             'Was line valid?
    76.             If m.Success Then
    77.  
    78.                 'Get tag and description
    79.                 Dim tag As String = m.Groups(1).Captures(0).Value
    80.                 Dim desc As String = m.Groups(2).Captures(0).Value
    81.  
    82.                 'Add the entry to the dictionary, unless an entry with tht tag is already present.
    83.                 If Not dict.ContainsKey(tag) Then dict.Add(tag, desc)
    84.  
    85.             End If
    86.  
    87.         Next
    88.  
    89.         Return dict
    90.  
    91.     End Function

    Usage:
    Code:
    TextBox1.Text = replacetags("I love '<tag1>'.", readtags(mytagfile))
    Regards Tom
    IT WORKS PERFECT!!! THANK YOU VERY MUCH!

    Thank you all for your patience and great help!

    THANK YOU SOOOO MUCH!

Tags for this Thread

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