-1

我正在尝试为以下任务找到解决方案,但我被卡住了。请寻求解决方案。该代码可以很好地计算精确的字符串匹配,但是我想要实现的是使用正则表达式模式或类似运算符和通配符的组合来查找和计算单词是否相同,任何可以接受的解决方案,例如:

地址

地址

地址

地址

地址

地址

地址

等等

计数=6

仅当输入 textbox.text 为“add?ress”时,计数应为 6(“add ress”因空格而被排除),为了清楚起见,输入可以是任何单词,但 if ? 介于两者之间,则应适用此规则。问号 (?) 可以是除字母和数字以外的任何内容,如果 textbox.text 为“add ress”count=1 或 textbox.text 为 add-ress count=1。我想用?对于字符串之间的任何字符(如果它出现在搜索文本框上)。

谢谢你。

Private Sub btnAddTerm_Click(sender As Object, e As EventArgs) Handles btnAddTerm.Click

    Dim foundAt As Integer = RichTextBox1.Find(txbSearch_Term.Text, 0, RichTextBoxFinds.WholeWord)
    Dim text2 As String = RichTextBox1.Text.ToString.ToLowerInvariant

    Dim txbsearch As String = txbSearch_Term.Text

    'Regex.Match(txbsearch, "[-]{1,2}:[/]{2}:[?]{2}")
    'Regex.Match(text2, "^.*?\\b([^a-zA-Z\\s].?[sa-zA-Z])\\b.*$", RegexOptions.None)

    txbSearch_Term.CharacterCasing = CharacterCasing.Lower

    Dim count As Integer = 0

    Do While foundAt > -1

        'count += Regex.Matches(text2, txbsearch.ToString()).Count
        'Regex.Match(txbsearch, "^.*?\\b([^a-zA-Z\\s].?[sa-zA-Z])\\b.*$")
        Dim pattern As String = "^.*?\b([^a-zA-Z\s].?[sa-zA-Z])\b.*$"
        RichTextBox1.SelectAll()
        'Dim input As String = txbsearch

        count += 1

            RichTextBox1.Select(foundAt, txbsearch.Length)
            RichTextBox1.SelectionBackColor = Color.Yellow

            foundAt = RichTextBox1.Find(pattern, foundAt + txbsearch.Length, RichTextBoxFinds.WholeWord)

    Loop

    Dim rowId As Integer = dgvTermCount.Rows.Add()
    Dim row As DataGridViewRow = dgvTermCount.Rows(rowId)
    row.Cells("Column1").Value = txbsearch
    row.Cells("Column2").Value = count

End Sub
4

2 回答 2

0

你忘记量化你的角色类别

\b([^a-zA-Z\s]+.?[sa-zA-Z]+)\b
              ^           ^

第二个字符类看起来很奇怪,前导 S 和所有..

于 2022-01-21T01:28:26.297 回答
0

我找到了解决方案。我为每个循环添加了问号,并用我每次从数组中需要的所有字符替换了问号。

    Dim charToReplace() = {"!", Chr(34), "#", "$", "%",
                                "&", "(", ")", "*", "+",
                                ",", "-", ".", "/", ":",
                                ";", "<", "=", ">", "@",
                                "[", "\", "]", "^", "_",
                                "`", "{", "|", "}", "~",
                                "'", " ", "?", "¤", "§", "æ", "ø"}
If txbsearch.Contains("?") Then
    Dim foundAtInt As Integer = 0
        For Each char_ As String In charToReplace
            foundAtInt += 1
            foundAtInt = RichTextBox1.Find(replaceinput.Replace("?", char_), 0, RichTextBoxFinds.WholeWord)
                Do While foundAtInt > -1
                    counting += 1
                    RichTextBox1.Select(foundAtInt, replaceinput.Length)
                    RichTextBox1.SelectionBackColor = Color.Yellow
                    foundAtInt = RichTextBox1.Find(replaceinput.Replace("?", char_), foundAtInt + replaceinput.Length, RichTextBoxFinds.WholeWord)
                Loop
        Next
End If
于 2022-02-01T17:27:07.707 回答