使用正则表达式模式计算字符串

Count string using regex pattern

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

Add.ress

add/ress

地址

地址

地址*地址

add!ress

add\ress

等等

计数=6

只有当输入 textbox.text 是“add?ress”时,计数才应该是 6(“add ress”因为 space 而被排除),为了明确输入可以是任何单词,但是如果?介于两者之间,则应适用此规则。 问号 (?) 可以是字母和数字以外的任何字符 else 如果 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

你忘记量化你的人品了classes

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

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

我找到了解决办法。我为每个循环添加了每次都用数组中需要的所有字符替换问号。

    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