RegEX Match All Lines Containing String

I have a text file read into a variable. The text file consists of a series of lines. Each line consists of words or phrases of more than one word separated by commas such as “cat,pig,pink fish,eagle”.

I am using the string variable R to represent a search word the user enters into a editfield.

If I want to find all lines where the search word is the first in the line this search pattern works:

“^” + R + “,.+$”

Searching for “cat” it correctly finds the lines:

“cat,pig,pink fish,eagle”
“cat,monkey,whale,elephant”

Now I want to find all lines whereas the search word can be anywhere within the line, not just the first word; however, the following pattern is not working:

“^." + R + ".$”

Searching for cat I want to find both these lines because they contain the word cat:

“cat,pig,pink fish,eagle”
“monkey,whale,cat,elephant”

You’ve got to do a while loop. Keep calling Search until it returns a Nil RegexMatch object.

That pattern is not ideal. For example, it would match “catfish” too. You also don’t want to allow raw input from users as they might inadvertently, or maliciously, insert meaningful regex tokens.

"^.*\\b\\Q" + R.ReplaceAllB( "\\E", "\\E\\\\E\\Q" ) + "\\E\\b.*"

Thanks for the input, I do have loop in place:

    While Match1 <> Nil
      Temp = Match1.SubExpressionString(0)
      Temp = ReplaceAll(Temp, EndofLine, "")
      Find1 = Find1 + Temp + "*"
      Match1 = RG.Search()
    Wend

The found lines are placed into string variable called Find1 and further parsed and displayed in a listbox; however, my problem is that the search pattern “^." + R + ".$” is not working. That pattern appears to be causing the loop to never end.

I tried the pattern “^.\b\Q" + R.ReplaceAllB( “\E”, “\E\\E\Q” ) + "\E\b.”, but comes up as a Find1 being an empty string.

Are you sure R and the original source are what you think they are?

I have verified both Search Term R (EditField.Text) and the Source (8.3MB Ascii text file loaded in App variable). The source text file uses the Chr13 & Chr10 EndofLine, but that was not the problem either as I am loading two other application resources from plain text files with the same format.

I tried the following pattern and it appears to obtain all the lines:

RG.SearchPattern = “^." + R + ".

For some reason it does want the the extra “$” at the end of the pattern, although I’ve used “^." + R + ".$” for other patterns where the text was divided by “/” instead of commas. Must be Gremlins.

It appears the problem was with EndofLine such as the EndofLine treating the Chr(13) & Chr(10) combination as two EndofLines instead of one. To remedy the situation I do not rely on EndofLine any longer but specifically check for Chr(10) and Chr(13).