Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch and replace in Richtextbox - Visual Basic
    primarykey
    data
    text
    <p>I have a richtextbox that I want to search and replace only WHOLE words. </p> <p>I have some code, however I have a feeling the way I have done it may not allow whole words only.</p> <p>CODE:</p> <pre><code> Dim search As String = TextBox1.Text Dim replace1 As String = TextBox2.Text Dim input As String input = Form1.RichTextBox1.Text.Trim Dim location As Integer search = search.Trim() replace1 = replace1.Trim() location = input.IndexOf(search) If location = -1 Then MsgBox("Text not found", MsgBoxStyle.Exclamation, "Not Found") Else Form1.RichTextBox1.Text = input.Remove(location, search.Length).Insert(location, replace1) MsgBox("Text " &amp; search &amp; " has been replaced with " &amp; replace1, , ) End If </code></pre> <p>If you have another way of doing it please share. </p> <p>Second attempt: Now works, however I want only whole words to be found.</p> <pre><code> Dim search As String = TextBox1.Text Dim replace1 As String = TextBox2.Text Dim input As String input = Form1.RichTextBox1.Text.Trim search = search.Trim() replace1 = replace1.Trim() If Regex.IsMatch(input, search) = True Then Dim out As String = Regex.Replace(input, search, replace1) Form1.RichTextBox1.Text = out MsgBox(search &amp; " has been replaced with " &amp; replace1, MsgBoxStyle.Information, "Success") Else MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found") End If </code></pre> <p>FINAL CODE: </p> <pre><code> Dim search As String = TextBox1.Text Dim replace1 As String = TextBox2.Text Dim input As String input = Form1.RichTextBox1.Text.Trim search = search.Trim() replace1 = replace1.Trim() If Regex.IsMatch(input, " " &amp; search &amp; " ") = True Then Dim out As String = Regex.Replace(input, " " &amp; search &amp; " ", " " &amp; replace1 &amp; " ") Form1.RichTextBox1.Text = out MsgBox(search &amp; " has been replaced with " &amp; replace1, MsgBoxStyle.Information, "Success") GoTo line Else GoTo line3 MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found") End If line3: If Regex.IsMatch(input, search &amp; " ") = True Then Dim out As String = Regex.Replace(input, search &amp; " ", replace1 &amp; " ") Form1.RichTextBox1.Text = out MsgBox(search &amp; " has been replaced with " &amp; replace1, MsgBoxStyle.Information, "Success") GoTo line Else GoTo line2 MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found") End If line2: If Regex.IsMatch(input, " " &amp; search) = True Then Dim out As String = Regex.Replace(input, " " &amp; search, " " &amp; replace1) Form1.RichTextBox1.Text = out MsgBox(search &amp; " has been replaced with " &amp; replace1, MsgBoxStyle.Information, "Success") GoTo line Else MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found") GoTo line End If line: </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload