if InStr(source_text, search_text) > 0 then
msgbox "it's a match!"
end if
if the source_text is–> “this is a test text and i have a numer 100 and 8759”
with InStr i get a match if i enter as the search_text “and 8759” or “text and”… etc
but i want a match if i enter as the search_text “test 8759”… or “have text 100 8759”
The initial title of the thread indicated that OP needed a push in the right direction (toward RegEx). I wasn’t being entirely unhelpful at that time.
@nicolscanessa We’ll need a better understanding of exactly what the desired result should do to be of any more assistance with RegEx. This is kind of why I suggest playing with RegExRX. You’ll learn about RegEx to help you in the future, while exploring and figuring out what you need for this case.
Otherwise, we’ll need a good sample of your input, with the desired and expected output / matching.
It’s not fancy, it’s from a beginner, there are more ways to do this:
Having 2 TextFields, one has source and the other the words to check, then a button for action and a Label3 for result
// in Button Action
checa(TextField1.Text, TextField2.Text.Split(" "))
Private Sub checa(source as string, anArray() as string) Label;.Text = "it's a match!"
Label3.Text = "it's a match!"
For i As Integer = 0 To anArray.Ubound
If InStr(source, anArray(i)) = 0 Then
Label3.Text = "No Match"
Exit
End If
Next
End Sub
Note: this works with information from first post, I’m sure it will not work if there are other cases. Remember that I’m still learning be careful of the code I post
Edit: this code will not work if testing abc in aabc source, it will say it’s a match and is only part of the full word. You will need to do some extra checking, like InStr value before/after the check is a space, the start/end of the source
the following RegEx pattern seems not to work for what I need
Dim rg As New RegEx
Dim myMatch As RegExMatch
rg.SearchPattern = "\\btest\\b8759"
'rg.SearchPattern = trim(txt_search.text)
'myMatch = rg.Search(Label1.text)
myMatch = rg.Search("this is a test text and i have a numer 100 and 8759")
If myMatch <> Nil Then
msgbox "it's a match!"
msgbox myMatch.SubExpressionString(0)
Else
msgbox "no match..."
End If
If you’re only interested in words, yes you need the \b anchor in a RegEx. That will prevent a match when, say, the user types “big” and the source text contains “embiggen”. However, the only way to do it is with Markus’ suggestion of splitting of the search term into works and cycling through them.
If you are letting the user define the terms though, you will want to escape their text so as to know cause errors. For example, if they were to type “(hi”, that would raise an exception. I suggest running each word though something like this:
Function EscapePattern (pattern As String) As String
pattern = "\\Q" + pattern.ReplaceAllB( "\\E", "\\\\EE\\Q" ) + "\\E"
pattern = pattern.ReplaceAll( "*", "\\E\\w*\\Q" )
return pattern
End
This will surround the terms with “\Q” and “\E” so that the term is taken literally, including escaping “\E” if the user typed it for some reason. It will also let them use “" as a wildcard so they could type "ac” and match “ac” or “abc” or “aaaaabc”. After escaping each work, surround the whole thing with “\b” at the beginning and end.