1

単語全体のみを検索して置換したいリッチテキストボックスがあります。

私はいくつかのコードを持っていますが、私が行った方法では単語全体しか許可されない可能性があると感じています。

コード:

    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 " & search & " has been replaced with " & replace1, , )
    End If

別の方法がある場合は、共有してください。

2 回目の試行: 動作するようになりましたが、単語全体のみを検索したいと考えています。

 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 & " has been replaced with " & replace1, MsgBoxStyle.Information, "Success")
    Else

        MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found")
    End If

最終コード:

   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 & " has been replaced with " & replace1, MsgBoxStyle.Information, "Success")
        GoTo line

    Else
        GoTo line3

        MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found")

    End If
line3:

    If Regex.IsMatch(input, search & " ") = True Then

        Dim out As String = Regex.Replace(input, search & " ", replace1 & " ")
        Form1.RichTextBox1.Text = out

        MsgBox(search & " has been replaced with " & replace1, MsgBoxStyle.Information, "Success")
        GoTo line

    Else
        GoTo line2
        MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found")

    End If
line2:

    If Regex.IsMatch(input, " " & search) = True Then

        Dim out As String = Regex.Replace(input, " " & search, " " & replace1)
        Form1.RichTextBox1.Text = out

        MsgBox(search & " has been replaced with " & replace1, MsgBoxStyle.Information, "Success")
        GoTo line
    Else

        MsgBox("Not found", MsgBoxStyle.Exclamation, "Not Found")
        GoTo line
    End If





line:
4

1 に答える 1

2
pattern = "\b" + Regex.Escape(search) + "\b";

if (Regex.Match(input, pattern).Success) {
  // found it
  Regex.Replace(input, pattern, replace1);
}

\bは単語の境界を示します。句読点やスペースの直後、または文字列の先頭に表示される単語と一致します...

http://www.regular-expressions.info/wordboundaries.html

                         

于 2012-10-28T20:54:41.200 に答える