VB.NET修改与Access数据库的匹配

VB.NET Modify Matching with Access Database

我在字典程序中工作

例如,如果我想从数据库中获取这一行:

我想用英文文本框写这个:

但如果我这样写:

相同没有逗号,但未找到匹配项

或除新行之外的任何其他修改

我修改代码使程序匹配句子失败

单词的数量,无论中间是否换行

翻译功能代码:

Function getexactsubtitles(ByVal content As String) As String
    Try
        Dim count As Integer = 0
        Dim connectionall As New OleDb.OleDbConnection(connectionString)
        Using adp As New OleDbDataAdapter
            Using tbl As New DataTable
                Using cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Subtitles]", connectionall)
                    cmd.Parameters.AddWithValue("@english", String.Concat("%", content, "%"))
                    adp.SelectCommand = cmd
                    If adp.Fill(tbl) > 0 Then
                        For Each row As DataRow In tbl.Rows
                            Dim en As String = row.Item("English").Replace("?", "\?")
                            Dim ar As String = row.Item("Arabic")
                            count += Regex.Matches(content, en, RegexOptions.IgnoreCase).Count
                            content = Regex.Replace(content, en, ar, RegexOptions.IgnoreCase)
                        Next
                    End If
                End Using
            End Using
        End Using
        MsgBox(IIf(count = 0, "لم يتم العثور على أى تطابق تام داخل قاعدة البيانات", "عدد الحالات المطابقة تماماً التى عثر عليها داخل قاعدة البيانات (" & " " & count & " " & ") من الجمل والكلمات"))
        Return content
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    getexactsubtitles = ""
End Function

请注意这部分:

Using cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Subtitles]", connectionall)
                        cmd.Parameters.AddWithValue("@english", String.Concat("%", content, "%"))

这是我尝试完成的工作,但由于某种原因它不起作用

翻译按钮代码:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = getexactsubtitles(TextBox1.Text)

如果我没理解错的话,您正在使用 Regex.Replace() 并且模式存储在 table 的第一列中。 您所需要的只是 " " 也匹配换行符。

我认为您应该考虑在您的模式中将 " " 替换为 \s+,因为它匹配以下任何字符:[\r\n\t\f ]More examples

\s+中的“+”表示重复一次或多次。这样就可以匹配

How are you [space] + [cr] + [lf]

today tommy?


解决方案

在您提供的示例中,

[English]                             |  [Arabic]
how\s+are\s+you\s+today\s+tommy\?     |  asking about tommy's health
  • Aternative:如果你不能在数据库中存储那个模式,在你的代码中替换它(这不是很有效)

    Dim en As String = row.Item("English").Replace("?", "\?").Replace(" ","\s+")
    


另外,我注意到你正在逃避问号。您还应该知道可能还有其他一些 metacharacters。所以请考虑改用 Regex.Escape()