Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I'm trying to find a solution for the following task, but I’m stuck. Please help me find a solution. The code is working fine for counting exact string match, but what I’m trying to achieve is to find and count words if they are not same using regex pattern or like operator and wildcard combined any solution acceptable for example.

Add.ress

add/ress

add-ress

add ress

add*ress

add!ress

add\ress

etc.

Count=6

Only and only if input textbox.text is "add?ress" count should be 6 (“add ress” excluded because of space) the question mark (?) can by anything except Letters and numbers else if textbox.text is “add ress” count=1 or if textbox.text is add-ress count=1. I would like to use ? for any char in between string if it appear on search textbox.

Thank you.

What I have tried:

VB
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
Posted
Updated 1-Feb-22 6:31am
v3

Quote:
I would like to use ? for any char in between string if it appear on search textbox.

You need to learn Regex better, '?' is not what you think it is.
Try
add.ress

Count is 6, "Add.ress" is rejected because of uppercase.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v2
Comments
Timi_z 21-Jan-22 6:58am    
Thank you for your reply Patrice T.
Count should be 6, but I got 0, nothing, function works only with exact match and is ok, additionally I want to add this feature to be able to match in this way.
The solution does not have to be a regular expression it can be like match or wildcard match or any other solution.
Text where I search is richtextbox text and the string to be searched is textbox.text
Patrice T 21-Jan-22 7:38am    
Show us your real code
Timi_z 23-Jan-22 9:26am    
The code that I have already posted is real code.
Patrice T 23-Jan-22 11:09am    
None of the RegEx in your code is to match the "add.ress".
I found a solution. I have added for each loop and replaced the question mark with all the char I needed from the array each time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900