RegEx question (or maybe something else?)

i use this code for search text

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”

how could i accomplish this?

Welcome to the wonderful world of RegEx!
I fully recommend Kem’s RegExRx to help, plus it’s only a fiver.

Can you explain what are you trying to do?

Do you want to know if all words are in the source_text without order or if there are words missing between them?

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.

I second Tim’s recommendation of RegExRX - don‘t even think of touching RegEx without it!

yes @Alberto De Poo … this is exactly what i need

i’m very grateful for everyone’s input. i’ve been reading about RegEx, but i can’t get the solution to this.


You can do it with InStr and a flag.

Split the search string into words -> array Words()

Iterate over all words in the array and check if they are in the searchText

If a word is NOT in there set the flag to False

If the flag is false exit the iteration (no point in going over it any more)

i think i need this–> \b (Matches a word boundary (outside [] only))

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

thanks… i will try it and let you know

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

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.

I have to test it intensively… but the following code seems to work

[code] dim flag as boolean = true
dim words_array() as string
words_array = split(trim(txt_search.text)," ")

for each w as string in words_array
if InStr(Label1.text,w) = 0 then
flag = false
exit for
end if

if flag = true then
msgbox “it’s a match!”
msgbox “:-< sorry… no match”
end if[/code]

Again, if you don’t mind matching words within words, this is the simplest.

let me thank everyone again for your comments… they have been very helpful.