|
-
Aug 8th, 2012, 06:28 AM
#1
Thread Starter
New Member
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.
-
Aug 8th, 2012, 09:38 AM
#2
Thread Starter
New Member
Re: Replacing Strings based on Text File
Does someone can help me?
Please!
-
Aug 8th, 2012, 10:15 AM
#3
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?
-
Aug 9th, 2012, 07:28 AM
#4
Thread Starter
New Member
Re: Replacing Strings based on Text File
 Originally Posted by dunfiddlin
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.
-
Aug 9th, 2012, 09:49 AM
#5
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?
-
Aug 10th, 2012, 06:14 AM
#6
Thread Starter
New Member
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.
-
Aug 10th, 2012, 09:10 AM
#7
Re: Replacing Strings based on Text File
You can try something like:
vb Code:
'Replaces tags in text and returns the result. Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String Dim parsedtext As New StringBuilder Dim indexst, indexgt, curindex As Integer Dim tag As String 'Reset current progress and get the location of first "<"c curindex = 0 indexst = text.IndexOf("<"c) While indexst > 0 'Append the text before the "<"c to the result. parsedtext.Append(text.Substring(curindex, indexst - curindex)) 'Find the first ">"c after the found "<"c indexgt = text.IndexOf(">"c, indexst) 'If no ">"c is found, we're done. If indexgt < 0 Then 'Append the rest of the text to the result parsedtext.Append(text.Substring(indexst)) Return parsedtext.ToString End If 'Retrieve the text between "<"c and ">"c tag = text.Substring(indexst + 1, indexgt - indexst - 1) 'Is the tag in the dictionary? If dict.ContainsKey(tag) Then 'Append the text associated with the tag to the result. parsedtext.Append(dict(tag)) Else 'Append <tag> to the result parsedtext.Append(text.Substring(indexst, indexgt - indexst + 1)) End If 'Update the current progress within the string curindex = indexgt + 1 'Find index of "<"c after the current index indexst = text.IndexOf("<"c, curindex) End While 'Append the last bit of text. parsedtext.Append(text.Substring(curindex)) Return parsedtext.ToString End Function 'Reads tags and descriptions from file and organizes them into a dictionary ordered by the tag. Private Function readtags(ByVal filename As String) As Dictionary(Of String, String) Static parse As Regex = New Regex("^\<(?<tag>[^\>]+)\>\s*=\s*(?<desc>.+)$", RegexOptions.Compiled) Dim lines() As String = File.ReadAllLines(filename) Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String) Dim m As Match For Each line As String In lines 'Try to validate the line. m = parse.Match(line) 'Was line valid? If m.Success Then 'Get tag and description Dim tag As String = m.Groups(1).Captures(0).Value Dim desc As String = m.Groups(2).Captures(0).Value 'Add the entry to the dictionary, unless an entry with tht tag is already present. If Not dict.ContainsKey(tag) Then dict.Add(tag, desc) End If Next Return dict 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)
-
Aug 10th, 2012, 11:09 AM
#8
Re: Replacing Strings based on Text File
vb.net Code:
Private Sub Submit_Click(sender As System.Object, e As System.EventArgs) Handles Submit.Click 'read the file Dim lines() As String = System.IO.File.ReadAllLines("tagfile.txt") 'set the text to a single line With RichTextBox1 .WordWrap = False .Multiline = False End With For Each line In lines 'check all tags Dim s() As String = Split(line, " = ") 's(0) = "<tag>", s(1) = "replacement text" 'replace all instances of current tag If RichTextBox1.Text.Contains(s(0)) Then RichTextBox1.Text = RichTextBox1.Text.Replace(s(0), s(1)) End If Next 'reset text box With RichTextBox1 .WordWrap = True .Multiline = True End With 'whatever else you want to do End Sub
Last edited by dunfiddlin; Aug 10th, 2012 at 11:46 AM.
Reason: simplified code after change of concept
-
Aug 10th, 2012, 11:11 AM
#9
Re: Replacing Strings based on Text File
 Originally Posted by ThomasJohnsen
You can try something like:
vb Code:
'Replaces tags in text and returns the result. 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.
-
Aug 10th, 2012, 11:24 AM
#10
Re: Replacing Strings based on Text File
 Originally Posted by dunfiddlin
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)
-
Aug 11th, 2012, 10:21 AM
#11
Thread Starter
New Member
Re: Replacing Strings based on Text File
 Originally Posted by ThomasJohnsen
You can try something like:
vb Code:
'Replaces tags in text and returns the result.
Private Function replacetags(ByVal text As String, ByVal dict As Dictionary(Of String, String)) As String
Dim parsedtext As New StringBuilder
Dim indexst, indexgt, curindex As Integer
Dim tag As String
'Reset current progress and get the location of first "<"c
curindex = 0
indexst = text.IndexOf("<"c)
While indexst > 0
'Append the text before the "<"c to the result.
parsedtext.Append(text.Substring(curindex, indexst - curindex))
'Find the first ">"c after the found "<"c
indexgt = text.IndexOf(">"c, indexst)
'If no ">"c is found, we're done.
If indexgt < 0 Then
'Append the rest of the text to the result
parsedtext.Append(text.Substring(indexst))
Return parsedtext.ToString
End If
'Retrieve the text between "<"c and ">"c
tag = text.Substring(indexst + 1, indexgt - indexst - 1)
'Is the tag in the dictionary?
If dict.ContainsKey(tag) Then
'Append the text associated with the tag to the result.
parsedtext.Append(dict(tag))
Else
'Append <tag> to the result
parsedtext.Append(text.Substring(indexst, indexgt - indexst + 1))
End If
'Update the current progress within the string
curindex = indexgt + 1
'Find index of "<"c after the current index
indexst = text.IndexOf("<"c, curindex)
End While
'Append the last bit of text.
parsedtext.Append(text.Substring(curindex))
Return parsedtext.ToString
End Function
'Reads tags and descriptions from file and organizes them into a dictionary ordered by the tag.
Private Function readtags(ByVal filename As String) As Dictionary(Of String, String)
Static parse As Regex = New Regex("^\<(?<tag>[^\>]+)\>\s*=\s*(?<desc>.+)$", RegexOptions.Compiled)
Dim lines() As String = File.ReadAllLines(filename)
Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim m As Match
For Each line As String In lines
'Try to validate the line.
m = parse.Match(line)
'Was line valid?
If m.Success Then
'Get tag and description
Dim tag As String = m.Groups(1).Captures(0).Value
Dim desc As String = m.Groups(2).Captures(0).Value
'Add the entry to the dictionary, unless an entry with tht tag is already present.
If Not dict.ContainsKey(tag) Then dict.Add(tag, desc)
End If
Next
Return dict
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|